从ASP Gridview中的Inputbox获取输入值

时间:2015-02-12 17:08:13

标签: javascript c# asp.net gridview

我有一个包含3个templatefield列的gridview。当gridview是数据绑定时,将在占位符所在的模板字段中生成输入文本框。

问题是,是否可以在回发后的代码隐藏文件中的输入框中获取值?或者是否需要使用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:未将对象引用设置为对象的实例。

这就是我生成inputBox的方法:

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 != "&nbsp;")
                {
                    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);
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

你试过这个吗?

TextBox compGenDesScoreTxt = GV.Rows [i] .Cells [3] .FindControl(&#34; controlID&#34;);

答案 1 :(得分:0)

想出来。这与GV的数据绑定有关。我正在使用占位符,然后在需要时用输入字段替换它们。而不是那样,我将输入文本字段直接放入aspx页面的模板字段中,并将可见性设置为false。然后,在数据绑定事件中,我将需要显示的字段设置为true。

将来可以帮助某人。