【发布时间】:2014-06-23 15:00:21
【问题描述】:
以这种方式在gridview中动态创建文本框:
int index = e.Row.RowIndex;
int tot = e.Row.Cells.Count;
for (int i = 1; i < tot; i++)
{
TextBox txtValor = new TextBox();
txtValor.Width = 15;
txtValor.BorderStyle = BorderStyle.Ridge;
txtValor.ID = "txt"+index.ToString()+i.ToString();
txtValor.Attributes.Add("runat", "server");
txtValor.Text = (e.Row.DataItem as DataRowView).Row[produ].ToString();
e.Row.Cells[i].Controls.Add(txtValor);
}
我无法获取值,已经尝试了这些方法:
quant = GridView1.Rows[i].Cells[j].Text;
quant = ((TextBox)(GridView1.Rows[i].Cells[j].Controls[0])).Text;
quant = ((TextBox)GridView1.Rows[i].Cells[j].FindControl("txt"+i.ToString()+j.ToString())).Text;
quant = GridView1.Rows[i].Cells[j].Text; // this way i get the value of the cell and not the textbox
Tgis 是代码隐藏的 ASPX:文本框是由行数据绑定创建的。
<asp:GridView ID="GridView1" runat="server" CellPadding="4" Font-Size="XX-Small" ForeColor="#333333" Width="100%" HorizontalAlign="Center" PageSize="35" OnRowDataBound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" ForeColor="#6E7265" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#BDC0C4" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#333333" Wrap="True" BorderStyle="Double" BorderWidth="1px" HorizontalAlign="Center" VerticalAlign="Middle" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" ForeColor="White" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" /
</asp:GridView>
【问题讨论】:
-
你能显示你的 ASPX gridView 标记吗?另外,您在哪种方法中创建新的 TextBox?
标签: c# asp.net gridview textbox