简体   繁体   English

将具有动态ID的值传递给C#中的代码

[英]pass value with a dynamic id to code behind in c#

Can anyone help to solve this? 谁能帮助解决这个问题? I am trying to pass a textbox's value to C# code behind, by using the product id of database as the textbox id. 我试图通过使用数据库的产品ID作为文本框ID,将文本框的值传递给后面的C#代码。
These code are coded under GridView of cart.aspx 这些代码被编码在cart.aspx的GridView下

     <asp:BoundField ItemStyle-CssClass="layoutBox" HeaderStyle-CssClass="gvHeader" DataField="prodID" HeaderText="PRODUCT" />
     <asp:TemplateField HeaderStyle-CssClass="gvHeader" HeaderText="SET QUANTITY">
       <ItemTemplate>
          <div id="divQty">
                <img id='Minus-<%# Eval("prodID") %>' class="qtyIcon" src="image/template/minus.jpg" onclick="minusQty(this.id)" />
                <input id='<%# Eval("prodID") %>' name='<%# Eval("prodID") %>' type="text" value='<%# Eval("qtyOrder") %>' oninput="noDecimal(this.id);" />
                <img id='Plus-<%# Eval("prodID") %>' class="qtyIcon" src="image/template/plus.jpg" onclick="addQty(this.id)" />
         </div>
      </ItemTemplate>
    </asp:TemplateField>

I had perform some javascript function and I have to set the id by using (prodID) store in database so I use //input type="text". 我已经执行了一些javascript函数,并且必须使用数据库中的(prodID)存储来设置ID,所以我使用// input type =“ text”。

Now I am facing a problem which is I can't access to the value of the textbox by its id. 现在,我面临的一个问题是我无法通过其ID访问文本框的值。 Below are the code behind--cart.aspx.cs: 以下是背后的代码-cart.aspx.cs:

I am retrieving all prodID from [Product] table because the textboxes are using them as id. 我正在从[Product]表中检索所有prodID,因为文本框使用它们作为id。

List<string> getProdID = new List<string>();

string sql1 = "SELECT prodID FROM [Product]";
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd1 = new SqlCommand(sql1, con);

con.Open();
SqlDataReader dr1 = cmd1.ExecuteReader();
while (dr1.Read()){
     getProdID.Add(dr1[0].ToString());
}
dr1.Close();
con.Close();

I tried to get the value of each textbox but come out an error: 'string' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?) 我试图获取每个文本框的值,但出现错误:'string'不包含'Value'的定义,并且找不到扩展方法'Value'接受类型为'string'的第一个参数(您是否丢失了?使用指令或程序集引用?)

for(int a = 0; a < getProdID.Count ; a++)
{
   int value = Request.Form[getProdID[a]].Value;
}

First of all, you need to have runat="server" for all controls to access it in code behind file.Please see below 首先,所有控件都需要具有runat =“ server”才能在文件后面的代码中访问它。请参见下文

<input id='<%# Eval("prodID") %>' name='<%# Eval("prodID") %>' type="text" value='<%# Eval("qtyOrder") %>' oninput="noDecimal(this.id);" runat="server" />

To access textbox in code behind, you can hook up OnRowDataBound event or just looping through GridView.Rows: 要访问后面代码中的文本框,您可以连接OnRowDataBound事件或仅循环访问GridView.Rows:

int indexOfYourTemplateColumn = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if(e.Row.RowType == DataControlRowType.DataRow) 
  {
   HtmlInputText txt = (e.Row.FindControl("id") as HtmlInputText);
   string txtValue = txt.Value;
  }
}

foreach(GridViewRow row in GridView1.Rows) {
 if(row.RowType == DataControlRowType.DataRow) {
   HtmlInputText txt = (row.FindControl("id") as HtmlInputText);
   string txtValue = txt.Value;
   }
 }

Hope it helps!! 希望能帮助到你!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM