2
Answers

No postback on ASP:gridview

Derek Hanekom

Derek Hanekom

8y
257
1
Hi,
 
 I am having a major issue with postback, on first load it works perfectly,
but when I select a value from the dropdownlist, my datagrid disappears.
I added EmptyDataText="No Data" and it displays.
 
<form id="contentForm" runat="server" ViewStateMode ="Enabled">

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false"

AllowPaging = "true" PageSize = "20" Font-Names = "Arial" EmptyDataText="No Data!"

Font-Size = "11pt" AlternatingRowStyle-BackColor = "skyblue" OnPageIndexChanging = "OnPaging"

HeaderStyle-BackColor = "#336699">

<Columns>


<asp:TemplateField>

<HeaderTemplate>

Customer Name:

<asp:DropDownList ID="ddlCustomerName" runat="server" EnableViewState="true"

OnSelectedIndexChanged = "CustomerChanged" AutoPostBack = "true"

AppendDataBoundItems = "true">

<asp:ListItem Text = "ALL" Value = "ALL"></asp:ListItem>

<asp:ListItem Text = "Top 10" Value = "10"></asp:ListItem>

</asp:DropDownList>

</HeaderTemplate>

<ItemTemplate>

<%# Eval("CustomerName") %>

</ItemTemplate>

</asp:TemplateField>

<asp:BoundField DataField="ServerName" HeaderText="Server Name" />

</Columns>

</asp:GridView>
</form> 
 
 
  protected override void OnInit(EventArgs e)

{

if (!IsPostBack)

{
Page.DataBind();
ViewState["Filter"] = "ALL";

BindGrid();

}

}


private void BindGrid()

{

DataTable dt = new DataTable();

String strConnString = "Server = 192.168.200.81\\NEW_VANTAGE; Database = MSD; User ID = sa; Password = [email protected]";

SqlConnection con = new SqlConnection(strConnString);

SqlDataAdapter sda = new SqlDataAdapter();

SqlCommand cmd = new SqlCommand("spx_GetCustomers");

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@Filter", ViewState["Filter"].ToString());

cmd.Connection = con;

sda.SelectCommand = cmd;

sda.Fill(dt);

GridView1.DataSource = dt;

GridView1.DataBind();

DropDownList ddlCustomer =

(DropDownList)GridView1.HeaderRow.FindControl("ddlCustomer");

this.BindCustomerList(ddlCustomer);


}




private void BindCustomerList(DropDownList ddlCustomer)

{

String strConnString = "Server = 192.168.200.81\\NEW_VANTAGE; Database = MSD; User ID = sa; Password = [email protected]";

SqlConnection con = new SqlConnection(strConnString);

SqlDataAdapter sda = new SqlDataAdapter();

SqlCommand cmd = new SqlCommand("select distinct CustomerName" +

" from ActiveIssue where type= 'AV Defender Status' group by CustomerName");

cmd.Connection = con;

con.Open();

ddlCustomer.DataSource = cmd.ExecuteReader();

ddlCustomer.DataTextField = "CustomerName";

ddlCustomer.DataValueField = "CustomerName";

ddlCustomer.DataBind();

con.Close();

ddlCustomer.Items.FindByValue(ViewState["Filter"].ToString())

.Selected = true;



}





protected void CustomerChanged(object sender, EventArgs e)

{

DropDownList ddlCustomer= (DropDownList)sender;

ViewState["Filter"] = ddlCustomer.SelectedValue;

GridView1.DataBind();


}




protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

ViewState["Filter"] = "ALL";

BindGrid();

}

}


protected void OnPaging(object sender, GridViewPageEventArgs e)

{

GridView1.PageIndex = e.NewPageIndex;

this.BindGrid();

}

}
}
 
 

Answers (2)