CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  ASP.NET

大虾,,帮忙啊,一下午了.button事件怎样取得gridview模板列中panel控件中的textbox的值.急啊....................

楼主hanchi8008(寒池)2006-07-03 16:41:07 在 .NET技术 / ASP.NET 提问

如题.  
                  int   n   =   Convert.ToInt16(MessId);     //   我可以得到我所选行的第一个字段值,也就是id  
                  Panel   panel   =   (Panel)this.GridView1.Rows[n].Cells[2].FindControl("Panel1");  
                  TextBox   text   =   (TextBox)panel.FindControl("TextBox1");  
  报错,  
  索引超出范围。必须为非负值并小于集合大小。  
  参数名:   index    
  谢谢!!!!!  
    Panel   panel   =   (Panel)this.GridView1.SelectedRow.Cells[2].FindControl("Panel1");  
                                            TextBox   text   =   (TextBox)panel.FindControl("TextBox1");  
                                            string   ReplyMess   =   text.Text.ToString();  
  这种方法取不到值啊?????///!!!!1  
  问题点数:20、回复次数:11Top

1 楼francsescoli(我爱世界杯)回复于 2006-07-03 16:53:04 得分 0

第一个字段值跟Rows[n]没有关系吧,n超出Rows.Count了。  
  直接使用RowCommand()方法不就行了?  
  GridView1_RowCommand(object   sender,   GridViewRowEventArgs   e)  
  {  
  int   n   =   e.Row.RowIndex;   //获取行号  
        Panel   panel   =   (Panel)this.GridView1.Rows[n].Cells[2].FindControl("Panel1");  
  TextBox   text   =   (TextBox)panel.FindControl("TextBox1");  
  ...  
  }  
  这样试试  
  Top

2 楼francsescoli(我爱世界杯)回复于 2006-07-03 16:57:11 得分 0

或  
  ?    
  第一个字段值跟Rows[n]没有关系吧,n超出Rows.Count了。  
  直接使用RowCommand()方法不就行了?  
  GridView1_RowCommand(object   sender,   GridViewRowEventArgs   e)  
  {  
  int   n   =   e.Row.RowIndex;   //获取行号  
  Panel   panel   =   (Panel)e.Row.Cells[2].FindControl("Panel1");  
  TextBox   text   =   (TextBox)panel.FindControl("TextBox1");  
  ...  
  }  
  这样试试Top

3 楼Eddie005(♂) №.零零伍 (♂)回复于 2006-07-03 16:59:50 得分 0

如果你的GridView控件设置了   AutoGenerateSelectButton="True",或者包含了一个命令为Selected的按钮列,那么下面的方法是可以取道数据的:  
    GridViewRow   row   =   this.GridView1.SelectedRow;  
                  string   tmp   =   ((TextBox)row.FindControl("TextBox1")).Text;  
                  Response.Write(tmp);  
   
  如果lz已经得到了MessId(不管你是用什么方法得到的),下面这样也可以取得:  
  string   MessId   =   "";  
                  GridViewRow   row   =   null;  
                  foreach   (GridViewRow   iRow   in   this.GridView1.Rows)  
                          if   (iRow.Cells[0].Text   =   MessId)   row   =   iRow;  
   
                  if   (row   !=   null)  
                  {  
                          string   tmp   =   ((TextBox)row.FindControl("TextBox4")).Text;  
                          Response.Write(tmp);  
                  }  
  Top

4 楼hanchi8008(寒池)回复于 2006-07-03 17:05:35 得分 0

l楼上的写在什么事件中呢?     buttonclick还是     rowcommand呢?  
  再楼上的没有e.Row.RowIndex.     谢谢!Top

5 楼hanchi8008(寒池)回复于 2006-07-04 11:53:14 得分 0

Top      
    Eddie005(♂)   №.零零伍   (♂)      
  在buttonclick中写还是取不到值的.Top

6 楼Eddie005(♂) №.零零伍 (♂)回复于 2006-07-04 11:58:34 得分 20

那就是整个页面逻辑的问题了,把整个页面的关键代码贴出来吧~Top

7 楼hanchi2002()回复于 2006-07-04 12:28:13 得分 0

protected   void   Page_Load(object   sender,   EventArgs   e)  
          {  
                  BindDate();                  
          }        
   
          public   string   ConvertType(string   type)  
          {              
                  if   (type   ==   "")  
                  {  
                          return   type   =   "<font   color=red>未回复</font>";  
                  }  
                  else  
                  {  
                          return   type   =   "已回复";  
                  }  
          }  
   
          public   string   ConvertType2(string   type)  
          {  
                  return   type   ==   ""   ?   "true"   :   "false";  
          }  
               
          protected   void   GridView1_SelectedIndexChanging(object   sender,   GridViewSelectEventArgs   e)  
          {                
                  GridViewRow   row   =   GridView1.Rows[e.NewSelectedIndex];  
   
                  //得到公文     ID,传递给 回复  
   
                  Session["MessId"]   =   row.Cells[0].Text.ToString();  
                  Panel   panel   =   (Panel)row.FindControl("Panel1");    
                 
                  panel.Visible   =   true;                          
                 
          }      
         
          public   void   GetClient(int   CompanyId)  
          {  
                  //   连接中间数据库   0.12   服务器     这是连接中间数据库,通过他在连接到客户的.  
   
                  DbCon   dconConnString   =   new   DbCon();  
                  SqlConnection   dconObj   =   dconConnString.ConnGwgl();  
                  dconObj.Open();  
   
                  string   sql1   =   "select   ID,Client,SqlName,SqlIP,SqlUserID,SqlPwd,WebUser   from   ClientSetting   where   ID="   +   CompanyId;  
                  SqlCommand   com1   =   new   SqlCommand(sql1,   dconObj);  
                  SqlDataReader   dr1   =   com1.ExecuteReader();  
   
                  if   (dr1.Read())  
                  {  
                          Session["id"]   =   dr1["id"].ToString();  
                          Session["client"]   =   dr1["Client"].ToString();  
                          Session["SqlName"]   =   dr1["SqlName"].ToString();  
                          Session["SqlIP"]   =   dr1["SqlIP"].ToString();  
                          Session["SqlUserID"]   =   dr1["SqlUserID"].ToString();  
                          Session["SqlPwd"]   =   dr1["SqlPwd"].ToString();  
                          Session["WebUser"]   =   dr1["WebUser"].ToString();  
                  }                  
                  dr1.Close();  
                  dconObj.Close();  
          }  
         
          protected   void   GridView1_PageIndexChanging(object   sender,   GridViewPageEventArgs   e)  
          {  
                  this.GridView1.PageIndex   =   e.NewPageIndex;  
                  BindDate();  
          }  
   
          protected   void   BindDate()  
          {  
   
                  string   a   =   this.DropDownList1.SelectedValue.ToString();  
   
                  if   (a   ==   "")  
                  {  
                          GetClient(1);  
   
                  }  
                  else  
                  {  
                          int   CompanyId   =   Convert.ToInt16(a);  
                          GetClient(CompanyId);  
                  }  
   
                  //   连接客户数据库  
   
                  string   connectionString   =   "Data   Source="   +   Session["SqlIP"]   +   ";Initial   Catalog="   +   Session["SqlName"]   +   ";User   ID="   +   Session["SqlUserID"]   +   ";Password="   +   Session["SqlPwd"];  
                  SqlConnection   con   =   new   SqlConnection(connectionString);  
                  con.Open();  
   
                  DataSet   ds   =   new   DataSet();  
                  //   string   sql   =   "select   ID,title,content,sender,recipientusername,recipientuserdept,inputdate,reid   from   senddate   order   by   inputdate   desc";  
                  string   sql   =   "SELECT   senddate.ID   AS   sendid,   senddate.title   AS   sendtitle,senddate.content   AS   sendcontent,   senddate.sender   AS   sendsender,senddate.recipientuserdept   AS   receivedept,senddate.recipientusername   AS   sendrecip,   senddate.inputdate   AS   senddate,senddate.reid   AS   sendreid,   reply.ID   AS   replyid,   reply.reid   AS   replyid,reply.content   AS   replycontent   FROM   senddate   LEFT   OUTER   JOIN   senddate   reply   ON   senddate.ID   =   reply.reid   WHERE   (senddate.reid   =   0)   ORDER   BY   senddate.inputdate   DESC";  
                  SqlDataAdapter   da   =   new   SqlDataAdapter(sql,   con);  
                  da.Fill(ds,   "senddate");  
                  GridView1.DataSource   =   ds.Tables["senddate"].DefaultView;  
                  //da.Fill(ds);  
                  //GridView1.DataSource   =   ds.Tables[0].DefaultView;    
                  GridView1.DataBind();  
   
                  con.Close();  
          }  
          protected   void   DropDownList1_SelectedIndexChanged(object   sender,   EventArgs   e)  
          {  
                        }  
         
       
          protected   void   Button1_Click(object   sender,   EventArgs   e)  
          {  
                 
                                      if   (Session["MessId"]   !=   null)  
                          {  
                                  string   MessId   =   Session["MessId"].ToString();  
   
                                                GridViewRow   row   =   null;  
                                  foreach   (GridViewRow   iRow   in   this.GridView1.Rows)  
                                          if   (iRow.Cells[0].Text   ==   MessId)  
                                                  row   =   iRow;  
   
                                  if   (row   !=   null)  
                                  {  
                                          Panel   panel   =   (Panel)row.FindControl("Panel1");//改一下这里  
                                                                              TextBox   text   =   (TextBox)panel.FindControl("TextBox1");  
                                          Session["ReplyMess"]   =   text.Text.ToString();  
                                  }                              
                             
                                  string   ReplyMess   =   Session["ReplyMess"].ToString();  
   
                                  string   connectionString   =   "Data   Source="   +   Session["SqlIP"]   +   ";Initial   Catalog="   +   Session["SqlName"]   +   ";User   ID="   +   Session["SqlUserID"]   +   ";Password="   +   Session["SqlPwd"];  
                                  SqlConnection   con   =   new   SqlConnection(connectionString);  
                                  con.Open();  
   
                                  string   strGetInfo   =   "select   title,sender,recipientusername   from   senddate   where   ID='"   +   MessId   +   "'";  
                                  SqlCommand   comInfo   =   new   SqlCommand(strGetInfo,   con);  
                                  SqlDataReader   drInfo   =   comInfo.ExecuteReader();  
   
                                  if   (drInfo.Read())  
                                  {  
                                          string   title   =   drInfo["title"].ToString();  
                                          string   senders   =   drInfo["recipientusername"].ToString();  
                                          string   recipientusername   =   drInfo["sender"].ToString();  
   
                                          //string   sql2   =   "insert   into   senddate   (title,content,sender,recipientusername,reid)   values   ('Re:"   +   title   +   "','回复:"   +   ReplyMess   +   "','"   +   senders   +   "','"   +   recipientusername   +   "','"   +   MessId   +   "')";  
                                          string   sql2   =   "insert   into   senddate   (title,content,sender,recipientusername,reid)   values   ('Re:"   +   title   +   "','回复:"   +   ReplyMess   +   "','"   +   senders   +   "','"   +   recipientusername   +   "','"   +   MessId   +   "')";  
                                          SqlCommand   com2   =   new   SqlCommand(sql2,   con);  
   
                                          //   要先关闭datareader,才能再运行com2  
   
                                          drInfo.Close();  
                                          com2.ExecuteNonQuery();  
                                  }  
                                  con.Close();  
                          }  
                  //   }       //   对应第一个if  
          }  
  我的button也是在模板的panel中的  
  点击的是他,回复按钮.Top

8 楼hanchi2002()回复于 2006-07-04 12:28:32 得分 0

<asp:DropDownList   ID="DropDownList1"   runat="server"   AutoPostBack="True"   DataSourceID="SqlDataSource1"  
                          DataTextField="Client"   DataValueField="ID"   OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">  
                  </asp:DropDownList><asp:SqlDataSource   ID="SqlDataSource1"   runat="server"   ConnectionString="Data   Source=192.168.0.12;Initial   Catalog=my_gwgl_II;User   ID=my_060408Open131;Password=4ea8c02780d32ff9"  
                            ProviderName="System.Data.SqlClient"   SelectCommand="SELECT   [ID],   [Client]   FROM   [ClientSetting]">  
                    </asp:SqlDataSource>  
                  &nbsp;  
                           
                  <asp:GridView   ID="GridView1"   runat="server"   AutoGenerateColumns="False"   Height="1px"   OnSelectedIndexChanging="GridView1_SelectedIndexChanging"   AllowPaging="True"   PageSize="5"   OnPageIndexChanging="GridView1_PageIndexChanging"   Width="712px"   CellPadding="4"   ForeColor="#333333"   GridLines="None">  
                          <Columns>  
                                  <asp:BoundField   HeaderText="序号"   DataField="sendid"   />  
                                  <asp:TemplateField   HeaderText="状态">  
                                          <ItemTemplate>  
                                                  <asp:Label   ID="Label2"   runat="server"   Height="31px"   Text='<%#ConvertType(DataBinder.Eval(Container.DataItem,"replyid").ToString())%>'  
                                                          Width="82px"></asp:Label>  
                                          </ItemTemplate>  
                                  </asp:TemplateField>  
                                  <asp:TemplateField   HeaderText="标题">  
                                          <ItemTemplate>  
                                                  <table   style="width:   188px">  
                                                          <tr>  
                                                                  <td   style="width:   205px">  
                                                                          <asp:Label   ID="Label1"   runat="server"   Text='<%#   Bind("sendtitle")   %>'   Width="179px"   Height="16px"></asp:Label>                                                    
                                                                           
                                                                          <asp:Panel   ID="Panel1"   runat="server"   Visible="False">  
                                                                          <asp:Label   ID="Label3"   runat="server"   Text='<%#   Bind("sendcontent")   %>'   Width="179px"   Height="16px"></asp:Label>  
                                                                          <asp:Label   ID="Label4"   runat="server"   Text='<%#   Bind("replycontent")   %>'   Width="179px"   Height="16px"></asp:Label>&nbsp;  
                                                                                  <asp:TextBox   ID="TextBox1"   runat="server"   Height="75px"   TextMode="MultiLine"   Width="168px"   Visible='<%#Convert.ToBoolean(ConvertType2(DataBinder.Eval(Container.DataItem,"replyid").ToString()))%>'></asp:TextBox><br   />  
                                                                          <asp:Button   ID="Button1"   runat="server"     Text="回复"   Visible='<%#Convert.ToBoolean(ConvertType2(DataBinder.Eval(Container.DataItem,"replyid").ToString()))%>'   CommandName="aa"   OnClick="Button1_Click"   />  
                                                                                  &nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;   <asp:Button   ID="Button2"   runat="server"   Text="保存"     Visible='<%#Convert.ToBoolean(ConvertType2(DataBinder.Eval(Container.DataItem,"replyid").ToString()))%>'/>  
                                                                                 
                                                                          </asp:Panel>  
                                                                          &nbsp;  
                                                                  </td>  
                                                          </tr>  
                                                  </table>  
                                          </ItemTemplate>  
                                  </asp:TemplateField>  
                                  <asp:BoundField   HeaderText="发送者"   DataField="sendsender"   />  
                                  <asp:BoundField   HeaderText="接收单位"   DataField="receivedept"   />  
                                  <asp:BoundField   HeaderText="接收人"   DataField="sendrecip"   />  
                                  <asp:BoundField   HeaderText="发送日期"   DataField="senddate"   />  
                                  <asp:CommandField   ShowSelectButton="True"   />  
                          </Columns>  
                          <RowStyle   Font-Size="Smaller"   BackColor="#EFF3FB"   HorizontalAlign="Left"   VerticalAlign="Top"   />  
                          <FooterStyle   BackColor="#507CD1"   Font-Bold="True"   ForeColor="White"   />  
                          <EditRowStyle   BackColor="#2461BF"   />  
                          <SelectedRowStyle   BackColor="#D1DDF1"   Font-Bold="True"   ForeColor="#333333"   />  
                          <PagerStyle   BackColor="#2461BF"   ForeColor="White"   HorizontalAlign="Center"   />  
                          <HeaderStyle   BackColor="#507CD1"   Font-Bold="True"   ForeColor="White"   Font-Size="X-Small"   />  
                          <AlternatingRowStyle   BackColor="White"   />  
                  </asp:GridView>Top

9 楼Eddie005(♂) №.零零伍 (♂)回复于 2006-07-04 13:46:27 得分 0

protected   void   Page_Load(object   sender,   EventArgs   e)  
          {  
                if(!IsPostBack)   BindDate();  
          }Top

10 楼hanchi2002()回复于 2006-07-04 13:55:47 得分 0

你太厉害了.佩服啊!可以取到值了.开始的效果是:点某一行的"选择"本行的第二列panel会显示,再点其他的,这行panel就会消失,点的其他行会显示.可现在是,任意点,都会显示的.Top

11 楼hanchi2002()回复于 2006-07-04 14:11:42 得分 0

上边问题解决了.非常感谢!Top

相关问题

关键词

得分解答快速导航

  • 帖主:hanchi8008
  • Eddie005

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo