question

GonzalezLunaEstebanEliud-3790 avatar image
0 Votes"
GonzalezLunaEstebanEliud-3790 asked YijingSun-MSFT answered

How I can use <%# Eval(" ") %> in C# codebehind?

Hi, I want to know how to add a dinamyc <%# Eval("") %> but in C#.
I have a DataGrid with SQL Connection and I have case that I can choose what Table can be show in GridView. If i add the Items in HTML, the items are static, but if I do it in C#, i can use 1 GridView and change the Items, but i don't know how to do a Dynamic Eval.

I do this when i have the items in C#:

 string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                 using (SqlConnection con = new SqlConnection(constr))
                 {
     using (SqlDataAdapter sda = new SqlDataAdapter(" select No_curse, Room, Time from AgS")
                     {
                         gvSesion.DataSource = null;
                         gvSesion.DataBind();
                         using (DataTable dt = new DataTable())
                         {
    
                             dt.Columns.Add("No_curse");
                             dt.Columns.Add("Room");
                             dt.Columns.Add("Time");
                             sda.Fill(dt);
                              gvCursos.DataSource = dt;
                              gvCursos.DataBind();
 }}}


But I want add the Eval propietie like this:

   <td><asp:Button ID="cmdView" runat="server" Text="+" CommandName="Select" /></td>
                     <td><asp:Label ID="No_curse" runat="server" Text='<%# Eval("No_curse") %>'></asp:Label></td>
                     <td><asp:Label ID="Room" runat="server" Text='<%# Eval("Room") %>'></asp:Label></td>
                     <td><asp:Label ID="Time" runat="server" Text='<%# Eval("Time") %>'></asp:Label></td>


dotnet-csharpdotnet-aspnet-general
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

According to the docs, the dataBinder.Eval() method parameters expect a data container and a name to identify a column in the data. Use standard variables to hold the column name. Simply define the variable in the code behind as a property. It's up to you to design code that sets these values.

You'll still run into issues if the number of columns vary. It would be better to create a dynamic GridView utility using Generics.

Google search


1 Vote 1 ·

1 Answer

YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered

Hi @GonzalezLunaEstebanEliud-3790 ,
I don't understand your requirement clearly.Why you want to use Eval() in the codebehind? Do you want to dynamic bind column with the gridview in the code behind?
If you want to do this,why you don't use AutoGenerateColumns=true? If you don't want to do this,please tell us your requirement clearly.
Best regards,
Yijing Sun


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.