English 中文(简体)
2. 从代码背后的公式中获取格格格格格格格格米值
原标题:Get gridview values from code behind

我想从密码背后的电网观点中获取一个隐蔽领域的价值,但不在<代码>_RowDataBound或任何其他类似方法中使用。 这里是我目前的法典(它是一种购物假想):

<asp:GridView ID="gvShoppingCart"
runat="server"
AutoGenerateColumns="False"
AllowPaging="True"
DataKeyNames="ID"
ShowFooter="true">
<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:HiddenField ID="lblProductID" runat="server" Text= <%# Eval("ProductID") %>  />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Product">
        <ItemTemplate>
            <asp:HyperLink ID="HyperLink1" runat="server" 
                NavigateUrl= <%# Eval("ProductID", "product_details.aspx?id={0}") %>  
                Text= <%# GetProduct(Eval("ProductID")) %> ></asp:HyperLink>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Quantity">
        <ItemTemplate>
            <asp:TextBox ID="txtQuantity" runat="server" Width="35" CssClass="input" onkeypress="return isNumberKey(event)" AutoPostBack="true" ontextchanged="txtQuantity_TextChanged"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

For the sake of brevity I removed certain fields since they are there only for the display. The Quantity field is there for the user to input a number to add a number of products to his cart. I wish to access the lblProductID label in the _TextChanged event. In this same event, I tried

ID = (Label)gvShoppingCart.FindControl(“lblProductID”);

but it didn t work and returns only a null value. What is the solution?

最佳回答

For each row in your GridView there is a HiddenField for the ProductID.

可以通过使用以下代码(假设<代码>Hidden Field(例如,在第一行)进入:

HiddenField hiddenFieldProductID = 
           (HiddenField)gvShoppingCart.Rows[0].Cells[0].FindControl("lblProductID");

string productID = hiddenFieldProductID.Value

// Do something with the value

Hope, this helps.

问题回答

Try to replace the HiddenField to a label or a textbox and set the visible attribute to false. I had tried this before and it works.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签