怎么获取GridView中TextBox的值
在GridView模板列中添加TextBox控件,在后台获取TextBox的值。但是却得不到输入后的值。请高手指点一下代码如下:前台:<asp:GridViewID="M...
在GridView模板列中添加TextBox控件,在后台获取TextBox的值。但是却得不到输入后的值。请高手指点一下
代码如下:
前台:
<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="ID" HeaderText="EMPID" />
<asp:BoundField DataField="Name" HeaderText="EMPNAME" />
<asp:BoundField DataField="Gender" HeaderText="EMPGENDER" />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="MyTextBox" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
后台:
protected void Button1_Click(object sender, EventArgs e)
{
String text;
TextBox tb =(TextBox) MyGridView.Rows[0].Cells[3].FindControl("MyTextBox");
text=tb.Text;
Response.Write(text);
}
你们的写的跟我写的后台代码都是一样的。
我的问题不是找不到控件,而是能找到Textbox控件,能用代码修改它显示的值,就是无法获取用户在浏览器中输入的值。
各位高手们,谁能分析一下出现这种问题的原因啊?或者我的代码到底哪里出了问题? 展开
代码如下:
前台:
<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="ID" HeaderText="EMPID" />
<asp:BoundField DataField="Name" HeaderText="EMPNAME" />
<asp:BoundField DataField="Gender" HeaderText="EMPGENDER" />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="MyTextBox" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
后台:
protected void Button1_Click(object sender, EventArgs e)
{
String text;
TextBox tb =(TextBox) MyGridView.Rows[0].Cells[3].FindControl("MyTextBox");
text=tb.Text;
Response.Write(text);
}
你们的写的跟我写的后台代码都是一样的。
我的问题不是找不到控件,而是能找到Textbox控件,能用代码修改它显示的值,就是无法获取用户在浏览器中输入的值。
各位高手们,谁能分析一下出现这种问题的原因啊?或者我的代码到底哪里出了问题? 展开
4个回答
推荐于2017-09-09
展开全部
GridView设置如下:
<asp:GridView ID="GridViewlb" runat="server" AutoGenerateColumns="False" CssClass="tabblue" ShowCellToolTip=" True"
EmptyDataText="暂时没有记录!" Width="100%" Height="100%" AllowPaging="True" OnPageIndexChanging="GridViewlb_PageIndexChanging"
DataKeyNames="id" PageSize="26" OnRowCreated="GridViewlb_RowCreated" OnRowDataBound="GridViewlb_RowDataBound"
AllowSorting="True">
<Columns >
<asp:BoundField DataField="id" HeaderText="编号" />
<asp:BoundField DataField="type" HeaderText="类型" />
<asp:BoundField DataField="item" HeaderText="项目" ></asp:BoundField>
<asp:BoundField DataField="detail" HeaderText="内容" >
<ItemStyle Width="100px" />
<ItemStyle Width="120px" />
</asp:BoundField>
<asp:BoundField DataField="basescore" HeaderText="基本分" />
<asp:BoundField DataField="stdevaluation" HeaderText="评分标准" />
<asp:TemplateField HeaderText="打分" >
<ItemTemplate>
<asp:TextBox id="textbox1" runat="server" Text="" Width="80%" Height="24px"></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="50px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
获得GridView中TextBox的值:
如果是模板列,可以利用:
string str = ((TextBox)(this.GridView1.Rows[行号].Cells[6].FindControl("textBox1"))).Text.Trim();
如果不是模板列,可以利用
string str = this.GridView1.Rows[行号].Cells[6].Text.Trim();
<asp:GridView ID="GridViewlb" runat="server" AutoGenerateColumns="False" CssClass="tabblue" ShowCellToolTip=" True"
EmptyDataText="暂时没有记录!" Width="100%" Height="100%" AllowPaging="True" OnPageIndexChanging="GridViewlb_PageIndexChanging"
DataKeyNames="id" PageSize="26" OnRowCreated="GridViewlb_RowCreated" OnRowDataBound="GridViewlb_RowDataBound"
AllowSorting="True">
<Columns >
<asp:BoundField DataField="id" HeaderText="编号" />
<asp:BoundField DataField="type" HeaderText="类型" />
<asp:BoundField DataField="item" HeaderText="项目" ></asp:BoundField>
<asp:BoundField DataField="detail" HeaderText="内容" >
<ItemStyle Width="100px" />
<ItemStyle Width="120px" />
</asp:BoundField>
<asp:BoundField DataField="basescore" HeaderText="基本分" />
<asp:BoundField DataField="stdevaluation" HeaderText="评分标准" />
<asp:TemplateField HeaderText="打分" >
<ItemTemplate>
<asp:TextBox id="textbox1" runat="server" Text="" Width="80%" Height="24px"></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="50px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
获得GridView中TextBox的值:
如果是模板列,可以利用:
string str = ((TextBox)(this.GridView1.Rows[行号].Cells[6].FindControl("textBox1"))).Text.Trim();
如果不是模板列,可以利用
string str = this.GridView1.Rows[行号].Cells[6].Text.Trim();
展开全部
String text=null;
text=((TextBox)(MyGridView.Rows[0].FindControl("MyTextBox"))).values.ToString().Trim();
Response.Write(text);
text=((TextBox)(MyGridView.Rows[0].FindControl("MyTextBox"))).values.ToString().Trim();
Response.Write(text);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
列:
protected void btnCommit_Click(object sender, EventArgs e)
{
Button bt1 = sender as Button;
if (bt1.CommandName.ToLower() == "bb")
{
foreach (GridViewRow gr in GridView1.Rows)
{
TextBox tb = (TextBox)gr.Cells[0].FindControl("txtPass");
string pass = bt1.CommandArgument.ToString();
string password = tb.Text.Trim();
Panel p1 = (Panel)gr.Cells[0].FindControl("pPassNone");
Panel p2 = (Panel)gr.Cells[0].FindControl("pPass");
if (password == pass)
{
p1.Visible = true;
p2.Visible = false;
}
}
}
}
protected void btnCommit_Click(object sender, EventArgs e)
{
Button bt1 = sender as Button;
if (bt1.CommandName.ToLower() == "bb")
{
foreach (GridViewRow gr in GridView1.Rows)
{
TextBox tb = (TextBox)gr.Cells[0].FindControl("txtPass");
string pass = bt1.CommandArgument.ToString();
string password = tb.Text.Trim();
Panel p1 = (Panel)gr.Cells[0].FindControl("pPassNone");
Panel p2 = (Panel)gr.Cells[0].FindControl("pPass");
if (password == pass)
{
p1.Visible = true;
p2.Visible = false;
}
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询