repeater列表中直接修改狀態

<asp:Repeater ID="RepeaterArticleList" runat="server" onitemdatabound="RepeaterArticleList_ItemDataBound">  
   <ItemTemplate>  
           <asp:HiddenField ID="HiddenField1" Value=<%#Eval("ID") %> runat="server" />  
  
    <asp:DropDownList ID="droplist1" runat="server" OnSelectedIndexChanged="DropDownList1_Change" AutoPostBack="true" >  
                  <asp:ListItem Text="啓用" Value="1"></asp:ListItem>  
                  <asp:ListItem Text="禁用" Value="2"></asp:ListItem>  
                  <asp:ListItem Text="刪除" Value="3"></asp:ListItem>  
         </asp:DropDownList>   
   </ItemTemplate>  
</asp:Repeater>  

 

cs代碼中對應的DropDownList1_Change方法,取到用戶ID,執行操做html

複製代碼
protected void DropDownList1_Change(object sender, EventArgs e)  
{
DropDownList drd = sender as DropDownList;
Repeater rps = drd.Parent.Parent as Repeater;
int n = ((RepeaterItem)drd.Parent).ItemIndex;
HiddenField hid = (HiddenField)(rps.Items[n].FindControl("HiddenField1"));
string userid= hid.Value;
string userState = Convert.ToInt32(drd.SelectedValue);

//已經拿到了id,能夠進行操做

YourFunction(userid,userState);
}
複製代碼

在前臺顯示的時候,再根據用戶的狀態值,來選擇下拉菜單的默認選中值首先爲RepeaterRepeater 控件添加事件 onitemdatabound="RepeaterArticleList_ItemDataBound" 接下來是完成代碼spa

 

protected void RepeaterArticleList_ItemDataBound(object sender, RepeaterItemEventArgs e)  
    {          
        DropDownList drd = (DropDownList)e.Item.FindControl("droplist1");  
        HiddenField hid = (HiddenField)(e.Item.FindControl("HiddenField1"));  
        //用你的方法經過ID獲得狀態值到userstate    
        string userstate = getUserStateByID(hid.Value);  
        drd.SelectedValue = userstate ;  
  
    }  
 
相關文章
相關標籤/搜索