Javascript 从ASP Gridview中的Inputbox获取输入值
我有一个包含3个templatefield列的gridview。当gridview进行数据绑定时,将在放置占位符的模板字段中生成一个输入文本框 问题是,是否可以在回发时从代码隐藏文件的输入框中获取值?还是需要用js在客户端完成 这是gridview的精简版本:Javascript 从ASP Gridview中的Inputbox获取输入值,javascript,c#,asp.net,gridview,Javascript,C#,Asp.net,Gridview,我有一个包含3个templatefield列的gridview。当gridview进行数据绑定时,将在放置占位符的模板字段中生成一个输入文本框 问题是,是否可以在回发时从代码隐藏文件的输入框中获取值?还是需要用js在客户端完成 这是gridview的精简版本: <asp:GridView ID="GV" runat="server" AutoGenerateColumns="False" CellPadding="3" DataSourceID="DS" Font-Siz
<asp:GridView ID="GV" runat="server"
AutoGenerateColumns="False" CellPadding="3"
DataSourceID="DS" Font-Size="X-Small" Width="100%" BackColor="White" CellSpacing="1"
BorderColor="#333333" BorderStyle="Inset" BorderWidth="1px"
ShowFooter="True" ondatabound="GV_DataBound"
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
</HeaderTemplate>
</asp:TemplateField>
<asp:BoundField DataField="SKILL" HeaderText="Skill" HtmlEncode="False" SortExpression="SKILL">
</asp:BoundField>
<asp:BoundField DataField="COMP_GEN" HeaderText="Competencies (General)" HtmlEncode="False"
SortExpression="COMP_GEN">
</asp:BoundField>
<asp:TemplateField HeaderText="Designer Score">
<ItemTemplate>
<asp:PlaceHolder runat='server' ID="devGenDesScore"></asp:PlaceHolder>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="devGenDesScoreTotal_txt" name="inputs" runat="server" Width="50px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#005293" Font-Bold="True" ForeColor="white" BorderColor="Gray"
BorderStyle="Solid" BorderWidth="1px" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="White" ForeColor="#253E51" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
它在int compGenDesScore=Convert….上抛出此错误。。。。行:
System.NullReferenceException:对象引用未设置为对象的实例
这是我生成输入框的方式:
protected void GV_DataBound(object sender, EventArgs e)
{
int c = GV.Columns.Count;
int r = GV.Rows.Count;
//Loop through each row
for (int i = 0; i < r; i++)
{
//Loop through each column on that row
for (int j = 0; j < c; j++)
{
//First competancy
if (j == 2)
{
//Read the contents of the cell, if its blank, do nothing. If it has text, add a textbox for the score
contents = GV.Rows[i].Cells[j].Text;
if (contents != " ")
{
PlaceHolder placeHolder = GV.Rows[i].FindControl("devGenDesScore") as PlaceHolder;
TextBox devGenDesScore_txt = new TextBox();
devGenDesScore_txt.ID = "devGenDesScore_txt";
devGenDesScore_txt.Style.Add("width", "50px");
placeHolder.Controls.Add(devGenDesScore_txt);
}
}
}
}
}
你试过这个吗
TextBox compGenDesScoreTxt=GV.Rows[i]。单元格[3]。FindControlId 找到了答案。这与GV的数据绑定有关。我使用占位符,然后在需要的地方用输入字段替换它们。相反,我将输入文本字段直接放在aspx页面的模板字段中,并将可见性设置为false。然后,在databind事件中,我将需要显示的字段设置为true
将来可能会对某人有所帮助。能否尝试查找textbox控件,字符串x=TextBoxGV.Rows[i].FindControldevGenDesScore.Text;抛出相同的错误。可能与文本框ID有关?我在问题中添加了更多代码,解释如何生成文本字段。传递给FindControl的ID应该是创建控件时分配给它的ID。再次尝试Anurag的代码,但使用正确的ID。当我尝试Anurag的代码时,我使用了创建字段时指定的ID,即字符串x=TextBoxGV.Rows[I]。FindControldevGenDesScore_txt.Text;
protected void GV_DataBound(object sender, EventArgs e)
{
int c = GV.Columns.Count;
int r = GV.Rows.Count;
//Loop through each row
for (int i = 0; i < r; i++)
{
//Loop through each column on that row
for (int j = 0; j < c; j++)
{
//First competancy
if (j == 2)
{
//Read the contents of the cell, if its blank, do nothing. If it has text, add a textbox for the score
contents = GV.Rows[i].Cells[j].Text;
if (contents != " ")
{
PlaceHolder placeHolder = GV.Rows[i].FindControl("devGenDesScore") as PlaceHolder;
TextBox devGenDesScore_txt = new TextBox();
devGenDesScore_txt.ID = "devGenDesScore_txt";
devGenDesScore_txt.Style.Add("width", "50px");
placeHolder.Controls.Add(devGenDesScore_txt);
}
}
}
}
}