当前位置:编程学习 > C#/ASP.NET >>

GridView的模板中我放了两个文本框怎么取里面的值



在GridView 里其中的一个模板里有两个输入框 和一个按钮, 
当我点击按钮的时候怎么取得到两个输入框的值啊。。。


求高人指点。。。
小弟刚找到个工作,第一份任务就是写这个,麻烦各位大哥帮帮小弟。 --------------------编程问答-------------------- 就是当我点击提交的时候怎么取得到两个输入框的值啊。。。求各位大哥帮帮忙 --------------------编程问答--------------------
    protected void btnSetUser_Click(object sender, EventArgs e)
    {
        string userid = this.UserID.Value;
        if (userid == null || userid == "") return;
        string message = "";
        foreach (GridViewRow row in gvProduct.Rows)
        {
            ///跳过非数据行
            if (row.RowType != DataControlRowType.DataRow) continue;
            CheckBox cbID = (CheckBox)row.FindControl("chk");
            ///产品ID
            HiddenField hfID = (HiddenField)row.FindControl("hidID");
            ///产品名称
            Label lab = (Label)row.FindControl("lb");
           
            ///起始日期
            TextBox fromDate = (TextBox)row.FindControl("txtFromDate");
            ///结束日期
            TextBox expirationDate = (TextBox)row.FindControl("txtExpirationDate"); --------------------编程问答-------------------- FindControl("控件名称")   --------------------编程问答-------------------- 给button的commandname指定一个值(select,edit)
然后再gridview的对应事件中,使用findcontrol。 --------------------编程问答--------------------

        protected void MyDGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("delete"))
            {
                MyDGridView.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text)...;


最好别用FindControl --------------------编程问答-------------------- string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Control[0]).Text.ToString();
string sex = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
string place = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text; --------------------编程问答-------------------- <td width="160px" align="center" height="40">
                   <asp:TextBox ID="txt_pri1" runat="server" Width="80" ValidationGroup="MKE"></asp:TextBox>
 <asp:TextBox ID="txt_pri2" runat="server" Width="80" ValidationGroup="MKE"></asp:TextBox>
                </td>
                <td width="80px" align="center">
                    <input type="checkbox"  name="chk_Sel" id="chk_Sel" onclick="addprice(this);" value='<%# Eval("id") %>'/>
                </td>

js代码:
 <script src="App_common/scripts/jquery-1.4.2.min.js" type="text/javascript"></script>

window.getvalue=function(obj)
{
var inputCk=$(obj);//取到当前点击事件的对象
var inputPrice1=inputCk.parent().prev().find('input').eq(0).val();//通过该对象找到它的父级的前一同级的td.找到下面的input;
var inputPrice2=inputCk.parent().prev().find('input').eq(0).val();
取到的值至于楼主想要在后台么得到就不用我说了
} --------------------编程问答-------------------- <input type="checkbox" name="chk_Sel" id="chk_Sel" command="sel" onclick="addprice(this);" value='buttom'/>

补充上面的..,每次点按钮.可以通过一个隐藏域来保存textbox的值....

 protected void MyDGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("sel"))
            {
//得到隐藏域hid的值...就相当于取到textbox的值
            }
--------------------编程问答--------------------  protected void Button1_Click(object sender, EventArgs e)
 {        
        String text;
        TextBox tb =(TextBox) MyGridView.Rows[0].Cells[列].FindControl("MyTextBox");
        text=tb.Text.Trim();
       

 }
--------------------编程问答-------------------- 给button设置一个属性CommandName="save",如:
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="save" OnClientClick="return confirm('确定要保存此修改吗?')" CommandArgument='<%# Eval("id")+","+(Container as RepeaterItem).ItemIndex%>'>修改</asp:LinkButton>

然后在判断:
if (e.CommandName == "save")
{
   string sex = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
   string place = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
} --------------------编程问答-------------------- 用JS应该也可以实现吧 --------------------编程问答-------------------- funtion addValue()
{
  //数据操作多在客户端执行...减轻服务器负担
} --------------------编程问答-------------------- 对了,忘了说,你的Text文本中还要设置EnableViewState="true" --------------------编程问答--------------------
引用 12 楼 nocallstle 的回复:
funtion addValue()
{
  //数据操作多在客户端执行...减轻服务器负担
}

 function addValue(obj) {
            var v1 = obj.parentNode.parentNode.childNodes[0].firstChild.value;
            var v2 = obj.parentNode.parentNode.childNodes[1].firstChild.value
            obj.value = parseInt(v1) ;
            obj.value=parseInt(v2);
        }
这样也可以获取的 --------------------编程问答--------------------     int numCount = 0;
接着在   RowDataBound加上行数
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            (e.Row.FindControl("Button1") as Button).CommandArgument = numCount.ToString();
              // 计算自动填充的行数
            numCount++;
        }

    }

然后就可以取了。
    protected void Button1_Click(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        int i = Convert.ToInt32(btn.CommandArgument);
        string bifen1 = (this.GridView1.Rows[i].FindControl("txtBifen1") as TextBox).Text;
        string bifen2 = (this.GridView1.Rows[i].FindControl("txtBifen2") as TextBox).Text;
        string id = (this.GridView1.Rows[i].FindControl("Label1") as Label).Text;
    } --------------------编程问答--------------------     int numCount = 0;
接着在   RowDataBound加上行数
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            (e.Row.FindControl("Button1") as Button).CommandArgument = numCount.ToString();
              // 计算自动填充的行数
            numCount++;
        }

    }

然后就可以取了。
    protected void Button1_Click(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        int i = Convert.ToInt32(btn.CommandArgument);
        string bifen1 = (this.GridView1.Rows[i].FindControl("txtBifen1") as TextBox).Text;
        string bifen2 = (this.GridView1.Rows[i].FindControl("txtBifen2") as TextBox).Text;
        string id = (this.GridView1.Rows[i].FindControl("Label1") as Label).Text;
    } --------------------编程问答-------------------- <asp:Button ID="btn" runat="server" CommandArgument='<%# Container.DataItemIndex %>' CommandName="Click">点击</asp:Button>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Click")
{
string txt = null;
            txt = ((TextBox)GridView1.Rows[Convert.ToInt32(e.CommandArgument.ToString())].FindControl("文本框ID")).Text.Trim();
}
} --------------------编程问答--------------------
引用 5 楼 zorrowust 的回复:
C# code

        protected void MyDGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("delete"))
            {
                MyDGridV……


... --------------------编程问答-------------------- MyDGridView.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text)

这没出来。就是这句 --------------------编程问答-------------------- FindControl("控件名称") 
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,