pita
pita

Reputation: 537

Read dropdownlist value from a gridview

I have a gridview with a dropdownlist column, and i enabled the paging function. The problem is every time after it turns to the next page, the selected value of the dropdownlist on the previous page is back to default value.

I tried to wrap the code with if(!ispostback), only the first page available other pages are disappear

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<CPDEmployee> employeelist = (List<CPDEmployee>)Cache["EmployeeList"];

            unverifiedlist.DataSource = employeelist;
            unverifiedlist.AllowPaging = true;
            unverifiedlist.PageSize = 10;
            unverifiedlist.DataBind();
        }
    }
protected void PageSelect_SelectedIndexChanged(object sender, EventArgs e)
{
    int page = int.Parse(PageSelect.SelectedItem.Text);
    unverifiedlist.PageIndex = page;
    DataBind();
}





 <asp:GridView ID="unverifiedlist" runat="server" AutoGenerateColumns="false" AllowSorting="true" AllowPaging="true" ViewStateMode="Enabled">
                        <Columns><asp:TemplateField HeaderText="Options" >
                                <ItemTemplate>
                                    <asp:DropDownList ID="options" runat="server" AutoPostBack="true">
                                        <asp:ListItem Value="1">Verified</asp:ListItem>
                                        <asp:ListItem Value="0">Rejected</asp:ListItem>
                                    </asp:DropDownList>
                                </ItemTemplate>
                             </asp:TemplateField>
                    </Columns>
                    <PagerSettings Visible="false"/>            
        </asp:GridView>
<asp:DropDownList ID="PageSelect" runat="server" AutoPostBack="true" OnSelectedIndexChanged="PageSelect_SelectedIndexChanged"></asp:DropDownList>

does anyone know how to fix it, where should I put ispostback? thanks

Upvotes: 0

Views: 2146

Answers (1)

Icarus
Icarus

Reputation: 63970

You need to handle OnRowDataBound and set the appropriate element programmatically. Example:

<asp:GridView ID="unverifiedlist" runat="server" 
   OnRowDataBound="unverifiedlist_RowDataBound" AutoGenerateColumns="false" 
    AllowSorting="true" AllowPaging="true" ViewStateMode="Enabled">

And implement something like:

protected void unverifiedlist_RowDataBound(Object sender, GridViewRowEventArgs e)
{
  if(e.Row.RowType == DataControlRowType.DataRow)
  {
     ((DropDownList)e.Row.FindControl("options")).SelectedValue=((CPDEmployee)e.Row.DataItem).Unverified;

  }
}

Assuming, obviously, that you have a Property called Unverified on your business object. You should use whatever is appropriate. This is just an example.

UPDATE:

Since the drop down inside the grid is auto posting back, I would add an event handler for OnSelectedIndexChanged to the drop down list inside the Grid. Something like:

<asp:DropDownList ID="options" runat="server" AutoPostBack="true" OnSelectedIndexChanged="options_SelectedIndexChanged">
  <asp:ListItem Value="1">Verified</asp:ListItem>
  <asp:ListItem Value="0">Rejected</asp:ListItem>
</asp:DropDownList>

And then

protected void options_SelectedIndexChanged(object sender, EventArgs e)
{ 
    string selecteValue = ((DropDownList)sender).SelectedValue;
    //Now persist this value in the appropriate business object  
    //this is the difficult part because you don't know for which row in the gridview
    //you are changing this selection. You'll need to devise a way to pass an extra 
    //value (an Employee ID, I would imagine) that would allow you to grab the 
    // record from the List<CDPEmployee> and change the property to the new selection.
}

Upvotes: 1

Related Questions