【问题标题】:Get Input value from Inputbox inside ASP Gridview从 ASP Gridview 中的 Inputbox 获取输入值
【发布时间】:2015-02-12 17:08:13
【问题描述】:

我有一个带有 3 个模板字段列的网格视图。当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:对象引用未设置为对象的实例。

这就是我生成输入框的方式:

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);
                }
            }
        }
    }
}

【问题讨论】:

  • 你能试着找到文本框控件吗,字符串 x=((TextBox)GV.Rows[i].FindControl("devGenDesScore")).Text;
  • 抛出同样的错误。可能与文本框ID有关吗?我在解释我如何生成文本字段的问题中添加了更多代码
  • 您传递给FindControl() 的ID 应该是您在创建控件时为其分配的ID。再次尝试 Anurag 的代码,但使用正确的 ID。
  • 当我尝试 Anurag 的代码时,我确实使用了创建字段时分配的 ID,即 string x=((TextBox)GV.Rows[i].FindControl("devGenDesScore_txt")).Text;

标签: javascript c# asp.net gridview


【解决方案1】:

你试过了吗?

文本框 compGenDesScoreTxt = GV.Rows[i].Cells[3].FindControl("controlID");

【讨论】:

    【解决方案2】:

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

    将来可能会帮助某人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-22
      相关资源
      最近更新 更多