>>>>原本想通过触发TEXTBOX就得到这条DATAGRID的ID ,但没成功how did you do it? or try<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<Script language="C#" runat="server">
void Page_Load(Object o, EventArgs e)
{
if (!IsPostBack)
{
SqlDataAdapter da = new SqlDataAdapter("select * from authors",
"Server=localhost;Database=pubs;UID=sa;PWD=;");
DataSet ds = new DataSet();
da.Fill(ds,"authors"); DataGrid1.DataSource = ds.Tables["authors"].DefaultView;
DataGrid1.DataBind();
}
}void TextChanged(Object sender, EventArgs e)
{
   TextBox tb = (TextBox)sender;
   DataGridItem dgi = (DataGridItem)tb.Parent.Parent;
   Response.Write("***" + dgi.ItemIndex + " is changed<BR>");
}</script>
<form runat=server>
<asp:datagrid id="DataGrid1" runat="server">
<Columns>
<ASP:TemplateColumn HeaderText="Textbox">
<ItemTemplate>
<ASP:TextBox id="txt" Text='<%# DataBinder.Eval(Container.DataItem, "au_lname") %>' Runat="server"
AutoPostBack=true OnTextChanged="TextChanged" />
</ItemTemplate>
</ASP:TemplateColumn>
</Columns>
</asp:datagrid>
</form>

解决方案 »

  1.   

    思归,我照你的方法做了,但在TEXTBOX中的数据一改变就提示DataGridItem dgi = (DataGridItem)tb.Parent.Parent;指定的转换无效,为什么?
    你能说一下TEXTCHANGE中的语句是什么意思吗?
      

  2.   

    I don't know your code, how do I know what's wrong, just copy my code and test it, it works on my machineTextChanged occurs when the content of the text box is changed upon server postback.
      

  3.   

    代码和你写的差不多,只是我的TEXTBOX里的TEXT是空的,我的DATAGRID绑定的是另一张表,代码如下:
    <% @ Import Namespace="System.Data" %>
    <% @ Import Namespace="System.Data.OleDb" %>
    <HTML>
    <HEAD>
    <title>处理</title>
    </HEAD>
    <body>
    <form runat="server" ID="Form1">
    <asp:DataGrid ID="grid1" Runat="server" OnItemCommand="click1" AllowPaging="True" DataKeyField="renumber" AutoGenerateColumns="False" PageSize="5" AllowCustomPaging="True" PagerStyle-Mode="NumericPages">
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <table border="1">
    <tr>
    <td>
    测试提交方式:<asp:Label Runat=server text=' <%# DataBinder.Eval(Container.DataItem,"remode")%>'></asp:Label>
    </td>
    <td>
    测试人员IP:<asp:Label Runat=server text='<%# DataBinder.Eval(Container.DataItem,"reuserip") %>'></asp:Label></td>
    </tr>
    <tr>
    <td colspan="2">
    测试内容:<asp:Label Runat=server text='<%# DataBinder.Eval(Container.DataItem,"redetail") %>'></asp:Label>
    </td>
    </tr>
    <asp:Panel ID="panel1" Runat="server">
    <tr>
    <td colspan="2">请处理人员填写以下内容--</td>
    </tr>
    <tr>
    <td colspan="2">
    <asp:TextBox TextMode="MultiLine"  AutoPostBack=True OnTextChanged=textchange ID="memo1" Runat="server" />
    </td>
    </tr>
    <tr>
    <td>处理者姓名:
    <asp:TextBox id="textbox1" Runat="server" />
    </td>
    <td><asp:Button Runat="server"  Text="提交"></asp:Button></td>
    </tr>
    </asp:Panel>
    <asp:Panel ID="panel2" Runat="server">
    <tr>
    <td>
    <asp:Label ID="label1" Runat="server" />
    </td>
    </tr>
    </asp:Panel>
    </table>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    </form>
    <script language="C#" runat="server">

    void Page_Load(Object Sender,EventArgs e)
    {

    string connstring=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath(".")+"..\\..\\systemtest.mdb;";
            OleDbConnection conn=new OleDbConnection(connstring);
          
            string sql1="Select * From project order by prono desc";
    DataSet ds1 = new DataSet();
        OleDbDataAdapter MyAdapter1 = new OleDbDataAdapter(sql1,conn);
        MyAdapter1.Fill(ds1,"project");
    string projectno="";
    Session["projectno"]=ds1.Tables[0].Rows[0]["prono"].ToString();

    string sql="SELECT * FROM test  where prono='" + Session["projectno"] + "' order by renumber desc";
    OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
    DataSet ds=new DataSet();
    da.Fill(ds,"test");

    if(!IsPostBack)
    {
    grid1.DataSource=ds;
    grid1.DataBind();
    }
    }


    void textchange(Object Sender,EventArgs e)
    {
    TextBox tbs=(TextBox)Sender;

    DataGridItem dgi=(DataGridItem)tbs.Parent.Parent;
    string renum=dgi.ItemIndex.ToString();

    Session["renum"]=renum;
    }

    //void selectchange(Object Sender,EventArgs e)
    //{
    //renum=grid1.DataKeys[grid1.SelectedIndex].ToString();
    //Session["renum"]=renum;
    //}

    void click1(Object Sender,DataGridCommandEventArgs e)
    {
    //Response.AddHeader("Refresh", "1");

    string textb=((TextBox)e.Item.FindControl("textbox1")).Text;
    string memm=((TextBox)e.Item.FindControl("memo1")).Text;
            
            Response.Write(Session["renum"]);
            string connstring=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath(".")+"..\\..\\systemtest.mdb;";
            OleDbConnection conn=new OleDbConnection(connstring);
    string sql="Insert Into dispose Values ('" + Session["projectno"] + "','" + Session["renum"] + "','" + textb + "','" + Request.UserHostAddress.ToString() + "','" + memm +"')";
    OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
    DataSet ds=new DataSet();
    da.Fill(ds,"dispose");

    ((Panel)e.Item.FindControl("panel1")).Visible=false;
            ((Panel)e.Item.FindControl("panel2")).Visible=true;
    ((Label)e.Item.FindControl("label1")).Text=textb;

    }
    </script>
    </body>
    </HTML>
      

  4.   

    your asp:TextBox  is inside another control :
    <asp:Panel ID="panel1" Runat="server">so you need to another Parent:DataGridItem dgi = (DataGridItem)tb.Parent.Parent.Parent;
      

  5.   

    原来如此,但......思归大哥,我需要的是和表中的序号相同,它得到的却是按DATAGRID自己的顺序的号,这怎么办?是不是应该和DATAGRID中的DATAKEYFIELD有关啊?
      

  6.   

    once you get ItemIndex, you can use
    string s = DataGrid1.DataKeys[dgi.ItemIndex]
    which will give you "renumber" for the current row
      

  7.   

    为什么TEXTBOX里的值改变一次,页面还刷新一次啊,怎么才能不刷新呢?
      

  8.   

    Thank you very very much!!!!!!!!!
    思归大哥,真的很感谢你!你好厉害,能不能向我们传授传授经验啊?小妹我以后还要多多想你请教!