还有findcontrol属性有什么用途?什么用到?

解决方案 »

  1.   

    看MSDN
    --------------------
    属性值
    DataList 控件中选定项的索引。备注
    使用 SelectedIndex 属性以编程的方式指定 DataList 控件中的选定项。还可以使用此属性确定选定项的索引。注意    SelectedIndex 属性从零开始。
    若要撤消选定一项,请将 SelectedIndex 属性设置为 -1。示例
    [C#] 下面的示例展示如何使用 SelectedIndex 属性指定 DataList 控件中的选定项。<%@ Page Language="C#" AutoEventWireup="True" %>
    <%@ Import Namespace="System.Data" %><html>
    <head>   <script runat="server">      ICollection CreateDataSource() 
          {
          
             // Create sample data for the DataList control.
             DataTable dt = new DataTable();
             DataRow dr;
     
             // Define the columns of the table.
             dt.Columns.Add(new DataColumn("Item", typeof(Int32)));
             dt.Columns.Add(new DataColumn("Qty", typeof(Int32)));
             dt.Columns.Add(new DataColumn("Price", typeof(double)));
     
             // Populate the table with sample values.
             for (int i = 0; i < 9; i++) 
             {
                dr = dt.NewRow();
     
                dr[0] = i;
                dr[1] = i * 2;
                dr[2] = 1.23 * (i + 1);
     
                dt.Rows.Add(dr);
             }
     
             DataView dv = new DataView(dt);
             return dv;      }
     
          void Page_Load(Object sender, EventArgs e) 
          {         // Load sample data only once, when the page is first loaded.
             if (!IsPostBack) 
             {
                ItemsList.DataSource = CreateDataSource();
                ItemsList.DataBind();
             }      }      void Item_Command(Object sender, DataListCommandEventArgs e) 
          {
            
             // Set the SelectedIndex property to select an item in the DataList.
             ItemsList.SelectedIndex = e.Item.ItemIndex;         // Rebind the data source to the DataList to refresh the control.
             ItemsList.DataSource = CreateDataSource();
             ItemsList.DataBind();      }   </script></head>
    <body>   <form runat="server">      <h3>DataList Select Example</h3>      Click <b>Select</b> to select an item.      <br><br>
           
          <asp:DataList id="ItemsList"
               GridLines="Both"
               CellPadding="3"
               CellSpacing="0"           
               OnItemCommand="Item_Command"
               runat="server">         <HeaderStyle BackColor="#aaaadd">
             </HeaderStyle>         <AlternatingItemStyle BackColor="Gainsboro">
             </AlternatingItemStyle>         <SelectedItemStyle BackColor="Yellow">
             </SelectedItemStyle>         <HeaderTemplate>            Items         </HeaderTemplate>
             
             <ItemTemplate>            <asp:LinkButton id="SelectButton" 
                     Text="Select" 
                     CommandName="Select"
                     runat="server"/>            Item <%# DataBinder.Eval(Container.DataItem, "Item") %>         </ItemTemplate>
                  
             <SelectedItemTemplate>            Item:
                <asp:Label id="ItemLabel" 
                     Text='<%# DataBinder.Eval(Container.DataItem, "Item") %>' 
                     runat="server"/>            <br>            Quantity:
                <asp:Label id="QtyLabel" 
                     Text='<%# DataBinder.Eval(Container.DataItem, "Qty") %>' 
                     runat="server"/>            <br>            Price:
                <asp:Label id="PriceLabel" 
                     Text='<%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}") %>' 
                     runat="server"/>         </SelectedItemTemplate>      </asp:DataList>   </form></body>
    </html>
      

  2.   

    FindControl
    -----------
    在当前的命名容器中搜索指定的服务器控件示例
    下面的示例定义了一个 Button1_Click 事件处理程序。在被调用时,此处理程序使用 FindControl 方法来在包含页上查找其 ID 属性为 TextBox2 的控件。如果找到了该控件,则使用 Parent 属性确定其父控件,并将父控件的 ID 写入页中。如果没有找到 TextBox2,则将 "Control Not Found" 写入页中。private void Button1_Click(object sender, EventArgs MyEventArgs)
    {
          // Find control on page.
          Control myControl1 = FindControl("TextBox2");
          if(myControl1!=null)
          {
             // Get control's parent.
             Control myControl2 = myControl1.Parent;
             Response.Write("Parent of the text box is : " + myControl2.ID);
          }
          else
          {
             Response.Write("Control not found");
          }
    }
      

  3.   

    利用如   ItemsList.DataSource = CreateDataSource();
             ItemsList.DataBind();
    这样的语句是不是就可以显示出datalist中itemtemplate所规定的式样?
      

  4.   

    通俗一点:
    FindControl就是查找用datagrid绑定的服务器控件,比如:
    TextBox teJia;
    teJia=(TextBox)searchproDataGrid.Items[i].FindControl("tj");
    上面能够得到绑定到datagird中的textbox内的值。
      

  5.   

    纠正一点,
    FindControl不仅仅是查找datagrid中的服务器控件
      

  6.   

    看看这个问题:
    <%@ Page Language="VB" ContentType="text/html"%>
    <%@ import namespace="system"%>
    <%@ import namespace="system.data"%>
    <%@ import namespace="system.data.sqlclient"%>
    <script language="VB" runat="server">
    sub page_load(sender as object,e as eventargs)
        reg.visible=true
    information.visible=false
    end sub
    sub retrieve()
        dim myconnection as sqlconnection
    myconnection=new sqlconnection("server=localhost;uid=sa;pwd=;database=lp")
    myconnection.open()
    dim adapter as sqldataAdapter
    dim sqlstr as string
    sqlstr="select * from message where id='" & username.text & "' and password='" & password.text & "'"
        adapter=new sqldataadapter(sqlstr,myconnection)
    dim DS as new dataset()
    adapter.fill(DS,"info")
    dim DT as datatable
    DT=DS.tables("info")
    if DT.rows.count=1 then
        dim DV as dataview=DT.defaultview
    display.datasource=dv
    display.databind()
    reg.visible=false
    information.visible=true
    else
        response.Write("输入有误,请检查用户名和密码!")
    end if
    myconnection.close()
    end sub
    sub clickit(sender as object,e as eventargs)
        retrieve()
    end sub
    sub datalist_itemcommand(sender as object,e as datalistcommandeventargs)
        information.visible=true
    reg.visible=false
    select case e.commandsource.text
        case "用户信息"
       display.selectedindex=e.item.itemindex
       retrieve()
    case "关闭"
       display.selectedindex=-1
       retrieve()
    end select
    end sub
    </script><html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>the using of datalist</title>
    </head>
    <body>
    <form runat="server">
    <asp:panel id="reg" runat="server" BackColor="#aabbcc">
    请输入用户姓名和密码:
    <blockquote>
    用户姓名:<asp:textbox id="username" runat="server"/><br>
    用户密码:<asp:textbox id="password" runat="server" TextMode="Password"/><p>
    <input type="button" id="button" value="确认" onClick="clickit"/>
    </blockquote>
    </asp:panel>
    <asp:panel id="information" runat="server" BackColor="#9933FF">
    <asp:datalist id="display" runat="server"> 
    <itemtemplate>
        <%# container.dataitem("id") %>
    <asp:linkbutton id="detail" runat="server" Text="用户信息"/>
    <asp:linkbutton id="edit" runat="server" Text="修改信息"/>
    </itemtemplate>
    <selecteditemtemplate>
        用户名字:<%# container.dataitem("id") %><br>
    用户爱好:<%# container.dataitem("fond") %><br>
    毕业院校:<%# container.dataitem("graduation") %><br>
    用户留言:<%# container.dataitem("leaveword") %><br>
    <asp:linkbutton id="title" runat="server" Text="关闭"/>
    </selecteditemtemplate>
    <edititemtemplate>
        用户名字:<asp:label id="nameofuser" runat="server"
                         Text=' <%# container.dataitem("id") %>' /><br>
    用户密码:<asp:textbox id="pwd" runat="server" TextMode="Password"/><br>
    重复密码:<asp:textbox id="repass" runat="server" textmode="password"/><br>
    用户爱好:<asp:textbox id="userfond" runat="server" TextMode="MultiLine"/><br>
    毕业院校:<asp:textbox id="gradu" runat="server" TextMode="SingleLine"/><br>
    用户留言:<asp:textbox id="userword" runat="server" TextMode="MultiLine"/><p>
    <asp:linkbutton id="update" runat="server" Text="更新"/>
    <asp:linkbutton id="cancel" runat="server" text="取消"/>
    </edititemtemplate>
    </asp:datalist>
    </asp:panel>
    </form>
    </body>
    </html>调试是老是弹出一个出错的对话框,有如下的提示:
    该网叶的问题可能使其无法正常显示或功能不正常。以后,双击显示在状态栏中的警告图标,就可以显示上述信息。
    点击上面的[隐藏详细信息]按钮后有如下显示:
    行:18
    字符:1
    错误:'clickit'未定义
    代码:0
    url:……我不明白明明已经定义了clickit,可是为什么还有这样的显示呢?请指教。谢谢。
      

  7.   

    试一下:
    <input type="button" id="button" value="确认" onClick="clickit" runat=server />
      

  8.   

    <input type="button" id="button" value="确认" onClick="clickit"/>
    =======>
    <input type="button" id="button" value="确认" onClick="clickit" runat="server"/>怎么不行,还有什么错误?
      

  9.   

    首先确保你已经在<input type="button" id="button" value="确认"/>上加了runat="server",如果还有错误,就把你执行此页面后的html源文件贴出来(浏览器-查看-源文件)
      

  10.   

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>the using of datalist</title>
    </head>
    <body>
    <form name="_ctl0" method="post" action="TMP7k7nrpu8j5.aspx" id="_ctl0">
    <input type="hidden" name="__VIEWSTATE" value="dDwtMTEzNjI0OTM3O3Q8O2w8aTwxPjs+O2w8dDw7bDxpPDM+Oz47bDx0PHA8cDxsPFZpc2libGU7PjtsPG88Zj47Pj47PjtsPGk8MT47PjtsPHQ8QDA8Ozs7Ozs7Ozs+Ozs+Oz4+Oz4+Oz4+Oz7TvOVbWDiAEY+E+CJBMtppX6yQpw==" /><div id="reg" style="background-color:#AABBCC;">

    请输入用户姓名和密码:
    <blockquote>
    用户姓名:<input name="username" type="text" id="username" /><br>
    用户密码:<input name="password" type="password" id="password" /><p>
    <input name="button" id="button" type="button" value="确认" onClick="clickit" />
    </blockquote> </div></form>
    </body>
    </html>
      

  11.   

    你可以认为这是DataBind()的参数.
    在DataBind()的时候,
    DataList/DataGrid 会把SelectedIndex,EditItemIndex指定的Item设置为指定的Style,
    并且使Item有被选中或被编辑的功能。
      

  12.   

    上楼:也就是说datalist.selectedindex可以显示selecteditemtemplate模版中的内容,而datalist.edititemindex则可以显示edititemtemplate模版中的内容吗?
      

  13.   

    xueyhfeng:
       我把onclick="clickit"改为onserverclick="clickit"后不再弹出那个错误提示框,不过点击[确认]按钮却没有任何反应。另外,将函数名clickit改为click也有一样的结果。
      

  14.   

    onClick ==>onServerClick你把源代码贴一下,
      

  15.   

    我改好了,以下可以运行:
    <%@ Page Language="VB" ContentType="text/html" Debug="true"%>
    <%@ import namespace="system"%>
    <%@ import namespace="system.data"%>
    <%@ import namespace="system.data.sqlclient"%>
    <script language="VB" runat="server">
    sub page_load(sender as object,e as eventargs)
        reg.visible=true
    information.visible=false
    end sub
    sub retrieve()
        dim myconnection as sqlconnection
    myconnection=new sqlconnection("server=localhost;uid=sa;pwd=;database=lp")
    myconnection.open()
    dim adapter as sqldataAdapter
    dim sqlstr as string
    sqlstr="select * from message where id='"& username.text &"' and password='"& password.text & "'"
        adapter=new sqldataadapter(sqlstr,myconnection)
    dim DS as new dataset()
    adapter.fill(DS,"info")
    dim DT as datatable
    DT=DS.tables("info")
    if DT.rows.count=1 then
        dim DV as dataview=DT.defaultview
    display.datasource=dv
    display.databind()
    reg.visible=false
    information.visible=true
    else
        response.Write("输入有误,请检查用户名和密码!")
    end if
    myconnection.close()
    end sub
    sub clickit(sender as object,e as eventargs)
        retrieve()
    end sub
    sub datalist_itemcommand(sender as object,e as datalistcommandeventargs)
        information.visible=true
    reg.visible=false
    select case e.commandsource.text
        case "用户信息"
       display.selectedindex=e.item.itemindex
       retrieve()
    case "关闭"
       display.selectedindex=-1
       retrieve()
        case "修改信息"
       display.editItemindex=e.Item.itemindex
       retrieve()
    case "取消"
       display.edititemindex=-1
       retrieve()
    case "更新"
       dim psd,repassword,ufond,grad,word as object
       dim tbx as object
       tbx=e.item.findcontrol("userfond")
       ufond=tbx
       tbx=e.item.findcontrol("gradu")
       grad=tbx
       tbx=e.item.findcontrol("userword")
       word=tbx
       tbx=e.item.findcontrol("pwd")
       psd=tbx
       tbx=e.item.findcontrol("repass")
       repassword=tbx
       if (psd.text<>repassword.text) then
           response.Write("您两次输入的密码不相同,请从新输入。")
       else 
           dim conn as sqlconnection
       conn=new sqlconnection("server=localhost;uid=sa;pwd=;database=lp")
       conn.open()
       dim cmd as sqlcommand
       dim sqlstr as string="update message set password='"& psd.text &"',fond='"& ufond.text &"',graduation='"& grad.text &"',leaveword='"& word.text &"' where id='"& username.text &"' and password='"& password.text &"'"                  
       cmd=new sqlcommand(sqlstr,conn)
       cmd.executenonquery()
       response.Write("更新成功!")
       try
       catch exp as sqlexception
       response.Write("更新失败!若需要更新,请从新开始。")
       end try
       conn.close()
       display.edititemindex=-1
       display.selectedindex=e.item.itemindex
    end if
    end select
    end sub
    </script><html>
    <head>
    <meta http-equiv="Content-Type" content="text/html">
    <title>the using of datalist</title>
    </head>
    <body>
    <form runat="server">
    <asp:panel id="reg" runat="server" BackColor="#aabbcc">
    请输入用户姓名和密码:
    <blockquote>
    用户姓名:<asp:textbox id="username" runat="server"/><br>
    用户密码:<asp:textbox id="password" runat="server" TextMode="Password"/><p>
    <asp:button ID="btn" runat="server" Text="确认" OnClick="clickit"/>
    </blockquote>
    </asp:panel>
    <asp:panel id="information" runat="server" BackColor="#aabbcc">
    <asp:datalist id="display" runat="server" OnItemCommand="datalist_itemcommand"> 
    <itemtemplate>
        <%# container.dataitem("id") %>
    <asp:linkbutton id="detail" runat="server" Text="用户信息"/>
    <asp:linkbutton id="edit" runat="server" Text="修改信息"/>
    </itemtemplate>
    <selecteditemtemplate>
        用户名字:<%# container.dataitem("id")%><br>
    用户爱好:<%# container.dataitem("fond")%><br>
    毕业院校:<%# container.dataitem("graduation")%><br>
    用户留言:<%# container.dataitem("leaveword")%><br>
    <asp:linkbutton id="title" runat="server" Text="关闭"/>
    </selecteditemtemplate>
    <edititemtemplate>
        用户名字:<asp:label id="nameofuser" runat="server" Text='<%# container.dataitem("id")%>'/><br>
    用户密码:<asp:textbox id="pwd" runat="server" TextMode="Password"/><br>
    重复密码:<asp:textbox id="repass" runat="server" textmode="password"/><br>
    用户爱好:<asp:textbox id="userfond" runat="server" TextMode="MultiLine"/><br>
    毕业院校:<asp:textbox id="gradu" runat="server" TextMode="SingleLine"/><br>
    用户留言:<asp:textbox id="userword" runat="server" TextMode="MultiLine"/><p>
    <asp:linkbutton id="update" runat="server" Text="更新"/>
    <asp:linkbutton id="cancel" runat="server" text="取消"/>
    </edititemtemplate>
    </asp:datalist>
    </asp:panel>
    </form>
    </body>
    </html>
      

  16.   

    xueyhfeng:
       有留言板的源码没?用数据库做数据源的,我对XML不懂。再次感谢一直以来你对我的帮助。此贴马上要结了。不过分数不多,谁叫我穷啊。