请问如何取得DataList中的TextBox!当我点击"修改"按钮的时候想得到当前行TextBox的值!我用这种方法不行,请高手帮我!!
public void DataList_ItemCommand(object sender,DataListCommandEventArgs e)
 {
if (e.CommandName=="Modify")
 {
    
    DataList1.EditItemIndex=-1;     ListBind();
 
    string a=((TextBox)e.Item.FindControl("ModifyButton")).Text;//这行报错!!

             Response.Write(a);
 }
}报错如下:Server Error in '/NetSystem' Application.
--------------------------------------------------------------------------------Specified cast is not valid. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: Specified cast is not valid.Source Error: 
Line 90:      ListBind();
Line 91:   
Line 92:      string a=((TextBox)e.Item.FindControl("ModifyButton")).Text;
Line 93: 
Line 94:  Response.Write(a);
 Source File: d:\netsystem\productcharacteristicadd.aspx.cs    Line: 92 

解决方案 »

  1.   

    string a=((TextBox)e.Item.FindControl("ModifyButton")).Text.ToString();
      

  2.   

    回:520zyb(学无止尽) ( ) 信誉:100Server Error in '/NetSystem' Application.
    --------------------------------------------------------------------------------Specified cast is not valid. 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: Specified cast is not valid.Source Error: 
    Line 86:     //{
    Line 87:   
    Line 88:  string a=((TextBox)e.Item.FindControl("ModifyButton")).Text.ToString();
    Line 89: 
    Line 90: 
     
      

  3.   

    在page_load事件中写
    if (!this.IsPostBack)  DataList1.DataBind();
    或 ListBind();
      

  4.   

    DataList1.EditItemIndex=-1;     ListBind();
    把这两句写在后面
      

  5.   

    DataList中,<asp:datalist id="DataList1" runat="server" Height="81px" Width="372px" BorderWidth="0px" ForeColor="Transparent" OnItemCommand="DataList_ItemCommand">
    我用的是:OnItemCommand="DataList_ItemCommand"是不是这里不对呢???
      

  6.   

    更新要用更新事件查查SDK,很简单的
      

  7.   

    在page_load事件中写
    if (!this.IsPostBack)了吗?
      

  8.   

    =((TextBox)e.Item.FindControl("ModifyButton")).Text;
    你这句findcontrol写的名字是button的名字,而要得到的事textbox,
      

  9.   

    所以应该这样写
    ((TextBox)e.Item.FindControl("TextBoxID")).Text.ToString().Trim();
    ………………
    DataList1.EditItemIndex=-1;
    ListBind();
      

  10.   

    关键是现在取不到ModifyButton的值呀!
      

  11.   

    你是要取Button的值,还是TextBox的值?
    取TextBox的值:
    ((TextBox)e.Item.FindControl("TextBoxID")).Text.ToString().Trim();
    取Button的值:((Button)e.Item.FindControl("ButtonID")).Text.ToString().Trim();转换要一致!
      

  12.   

    寒,你的textbox的id是ModifyButton这个么?
    如果你要找button那么((ImageButton)e.Item.FindControl("ModifyButton"))
      

  13.   

    是的,我这样写总不该会有错吧!!
    string a=((TextBox)e.Item.FindControl("ModifyButton")).Text;
      

  14.   

    我的ModifyButton是TextBox的ID,我想得到TextBox的值!
      

  15.   

    应该在编辑状态下才能取得TEXTBOX的值,你还是用EditCommand来做吧.
    你已经用DataList1.EditItemIndex=-1把当前行取消了,应该在操作完后再指定.
      

  16.   

    如果名字id是textbox的没错的话,这句就应该没有错。
      

  17.   

    是不是datalist 不能用Textbox,为何取不取值呢,,,,,