从 ASP Gridview 中的 Inputbox 获取输入值
Get Input value from Inputbox inside ASP Gridview
我有一个包含 3 个模板字段列的网格视图。当 gridview 是数据绑定时,在占位符所在的模板字段中生成一个输入文本框。
问题是,是否可以从post上的代码隐藏文件中的输入框中取回值?还是需要用js在客户端完成?
这是 gridview 的精简版:
<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>
我试过这个的变体,但无法让它工作:
TextBox compGenDesScoreTxt = GV.Rows[i].Cells[3].Controls[0] as TextBox;
int compGenDesScore = Convert.ToInt32(compGenDesScoreTxt.Text);
它在 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].Cells[3].FindControl("controlID");
想通了。这与 GV 的数据绑定有关。我使用占位符,然后在需要时用输入字段替换它们。取而代之的是,我将输入文本字段直接放入 aspx 页面的模板字段中,并将可见性设置为 false。然后,在数据绑定事件中,我将需要显示的字段设置为 true。
以后可能会对某人有所帮助。
我有一个包含 3 个模板字段列的网格视图。当 gridview 是数据绑定时,在占位符所在的模板字段中生成一个输入文本框。
问题是,是否可以从post上的代码隐藏文件中的输入框中取回值?还是需要用js在客户端完成?
这是 gridview 的精简版:
<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>
我试过这个的变体,但无法让它工作:
TextBox compGenDesScoreTxt = GV.Rows[i].Cells[3].Controls[0] as TextBox;
int compGenDesScore = Convert.ToInt32(compGenDesScoreTxt.Text);
它在 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].Cells[3].FindControl("controlID");
想通了。这与 GV 的数据绑定有关。我使用占位符,然后在需要时用输入字段替换它们。取而代之的是,我将输入文本字段直接放入 aspx 页面的模板字段中,并将可见性设置为 false。然后,在数据绑定事件中,我将需要显示的字段设置为 true。
以后可能会对某人有所帮助。