Reputation: 10777
I am trying to delete a row from a gridview if "Web drop course" option is selected. Here is the UI:
And the code:
for (int i = 0; i < showCourses.Rows.Count; i++)
{
if (((DropDownList)showCourses.Rows[i].FindControl("actionmenu")).SelectedValue == "1")
{
dropList.Add(showCourses.Rows[i].Cells[2].Text +showCourses.Rows[i].Cells[3].Text );
}
}
Here is the dropdown list:
<asp:ListItem Selected="True" Value="0">No Action</asp:ListItem>
<asp:ListItem Value="1">Web Drop Course</asp:ListItem>
The problem is, ((DropDownList)showCourses.Rows[i].FindControl("actionmenu")).SelectedValue
always returns 0 whether I choose No action
or Web drop course
. Can anyone see the problem?
Thanks
Upvotes: 0
Views: 294
Reputation: 18018
Considering your previous post, you are rebinding the gridview on each postback. Wrap those lines with a !IsPostback
conditional. Better wrap those into a method (say PopulateGrid()
) and call it. Then, you can re-call that method in other situations where you might need to rebind the data (OnPageIndexChanged
for example). Change your Page_Load
method like this:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
PopulateGrid();
}
}
private void PopulateGrid()
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = Userfunctions.GetConnectionString();
con.Open();
string query = "select * from RegisterTable where StudentID='" + MyGlobals.currentID + "'";
SqlDataAdapter adap = new SqlDataAdapter(query, con);
DataTable tab = new DataTable();
adap.Fill(tab);
showCourses.DataSource = tab;
showCourses.DataBind();
}
}
Upvotes: 1
Reputation: 1899
You are most likely not protecting against rebinding your data on postback. When your event that causes postback fires, the page load event fires before this. If you are binding in page load without a check for postback, you are basically resetting your data and then going into your event handler.
The page life cycle might be a good read: Page Life Cycle
Upvotes: 2