页面加载时如何从后台代码动态设置GridView TemplateField标签的绑定字段?
Got it, let's tackle this problem. You want to dynamically switch between binding the TMO1 or TMO2 database field to your lblMedia label based on a condition when the page loads—replicating that VB-style field name concatenation for data binding. Here are two reliable approaches to make this work:
Approach 1: Use GridView's RowDataBound Event (Most Flexible)
This method gives you full control over each row's data binding logic, making it great for complex conditions or additional processing.
Step 1: Simplify the TemplateField
First, remove the inline binding expression from your label since we'll handle the value assignment in code-behind:
<asp:TemplateField HeaderText="Media"> <ItemTemplate> <asp:Label ID="lblMedia" runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField>
Step 2: Add RowDataBound Logic in Code-Behind
In your VB code-behind, handle the RowDataBound event to set the label's text based on your condition:
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound ' Only process data rows (skip headers/footers) If e.Row.RowType = DataControlRowType.DataRow Then ' Get the data item for the current row Dim dataItem As DataRowView = CType(e.Row.DataItem, DataRowView) Dim lblMedia As Label = CType(e.Row.FindControl("lblMedia"), Label) ' Replace this with your actual condition to choose TMO1 or TMO2 Dim shouldUseTMO1 As Boolean = True ' e.g., based on a session variable, query param, etc. ' Fetch the appropriate field value Dim targetValue As Object = If(shouldUseTMO1, dataItem("TMO1"), dataItem("TMO2")) ' Apply your existing formatting logic lblMedia.Text = If(targetValue Is DBNull.Value, "-", GralFunctions.Format(targetValue)) End If End Sub
Approach 2: Dynamic Binding with a Backend Property (Cleaner Inline)
If you prefer keeping the binding expression in the markup, you can define a property in code-behind to hold the target field name, then reference it in your Eval call.
Step 1: Define a Public Property in Code-Behind
Add a property to store the field name you want to bind, and set its value in Page_Load:
' Public property accessible to the markup Public Property TargetMediaField As String = "TMO1" ' Default to TMO1 Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load ' Set the target field based on your condition here TargetMediaField = If(someCondition, "TMO1", "TMO2") ' Example: TargetMediaField = If(Request.QueryString("UseTMO2") = "True", "TMO2", "TMO1") End Sub
Step 2: Update the TemplateField Binding Expression
Use DataBinder.Eval to dynamically reference the field from your property:
<asp:TemplateField HeaderText="Media"> <ItemTemplate> <asp:Label ID="lblMedia" runat="server" Text='<%# If(DataBinder.Eval(Container.DataItem, TargetMediaField) Is DBNull.Value, "-", GralFunctions.Format(DataBinder.Eval(Container.DataItem, TargetMediaField))) %>'> </asp:Label> </ItemTemplate> </asp:TemplateField>
Which Approach to Choose?
- Use Approach 1 if you need to add extra logic (like validation, additional formatting, or conditional styling) alongside the field selection.
- Use Approach 2 for a more concise markup-focused solution when your condition is straightforward.
内容的提问来源于stack exchange,提问作者Juan Cerezo

