Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two drop down list and both's AutoPostBack property is set to true second dropdown list bind on selected index changed event of first dropdown list and i want to show grid on selected index changed event of second dropdown list but when i am trying select item from second dropdownlist that dropdown by default select first item every time and i am working in master page so please give me solution for that.
Posted
Comments
norbertabone 1-Sep-18 7:05am    
You may wish to check that the values of the Items in the dropdown are unique.If they are not unique it will always select the first item on post back. I had a similar case and found that out, after several weeks of frustration.

1.Because your controls have AutoPostBack property on true, every type when you select a new item in any of them a Postback is generated and the entire page is sent to the web server and there the page events are executed (PreInit, Init, Load, etc). So maybe in your Page_Load event your controls are refilled with data and the first index is selected as default.

2.In order to solve this you should the the init part only first time (if the page is not post back) like in the next example:
C#
protected void Page_Load(object sender, EventArgs e)
        {
                if (!Page.IsPostBack)
                {
                     //Here put your code for init/binding you drop down list controls!!!
                }
          }
 
Share this answer
 
Comments
Dhananjay32 25-Sep-14 1:31am    
Dear Raul this is not my requirement i already bind first dropdown list on page load and i also bind second dropdown list on selected index changed event of first dropdown list but i want gridview on selected index changed event of second dropdownlist but when i am trying to do that second drop down list select first item by default
Raul Iloc 25-Sep-14 1:43am    
1.Then in you drop down list index changed you have to take care to do the init of your second drop down list in the IF block like I suggested in my solution.
2.Could you show me also your C# code for events handling?
Dhananjay32 25-Sep-14 1:49am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using BOL;

public partial class HR_BankReport : System.Web.UI.Page
{
BankReport objBankReport = new BankReport();

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bndPartnerMaster();
bndBankName();
}
btn_Excel.Visible = false;
btn_Word.Visible = false;
}
protected void btn_Report_Click(object sender, EventArgs e)
{
//btn_Excel.Visible = true;
//btn_Word.Visible = true;
}

public void bndPartnerMaster()
{
DataSet ds = new DataSet();

ds = objBankReport.GetPartnerMaster();

ddl_Partner.DataSource = ds;
ddl_Partner.DataValueField = "Pid";
ddl_Partner.DataTextField = "Pname";
ddl_Partner.DataBind();
ddl_Partner.Items.Insert(0, new ListItem("---Select---", "0"));
}

public void bndBankName()
{
DataSet ds = new DataSet();

ds = objBankReport.GetBankNameForReport();
ddl_Bank.DataSource = ds;
ddl_Bank.DataValueField = "BankName";
ddl_Bank.DataTextField = "BankName";
ddl_Bank.DataBind();
ddl_Bank.Items.Insert(0, new ListItem("---Select---", "0"));
}

protected void ddl_Partner_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet ds = new DataSet();

objBankReport.PartnerName = ddl_Partner.SelectedItem.Text;
ds = objBankReport.GetBillNoByPartner();

ddl_Bill.DataSource = ds;
ddl_Bill.DataValueField = "SalaryId";
ddl_Bill.DataTextField = "BillNo";
ddl_Bill.DataBind();
ddl_Bill.Items.Insert(0, new ListItem("---Select---", "0"));
}
protected void ddl_Bill_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet ds = new DataSet();
if (ddl_Bill.SelectedValue != "0")
{
objBankReport.PartnerName = ddl_Partner.SelectedItem.Text;
objBankReport.BillNo = ddl_Bill.SelectedItem.Text;

ds = objBankReport.GetDateByPertnernBillNo();
txt_Month.Text = ds.Tables[0].Rows[0]["Date"].ToString();
}
else
{
txt_Month.Text = "";
}
}
//protected void ddl_Bill_SelectedIndexChanged1(object sender, EventArgs e)
//{

//}
}
Raul Iloc 25-Sep-14 2:18am    
1.You code regarding DDL selection seems to be OK, except that you have some "new DataSet()" that should not be there.
2. Try to debug and put breakpoints at lest in "SelectedIndexChanged" events of both DDL to see if ddl_Partner_SelectedIndexChanged is generated even when you select an item from the second DDL!
Dhananjay32 25-Sep-14 2:36am    
This problem occurs only for second item of second dropdown list but when i select third
item it should worked for me and that selected third item as it is shown in dropdown so which mistake i did ?
ASP.NET
<asp:updatepanel id="UpdatePanel1" runat="server" xmlns:asp="#unknown">
       <contenttemplate>
           <div>
               <br />
               <center>
                   <h2>
                       Bank Report</h2>
               </center>
               <table style="margin: 50px 0 0 25%;">
                   <tr>
                       <td>
                           <asp:label text="Partner Name" runat="server" cssclass="Label"></asp:label>
                       </td>
                       <td>
                           <asp:dropdownlist id="ddl_Partner" runat="server" autopostback="true">
                               onselectedindexchanged="ddl_Partner_SelectedIndexChanged">
                           </asp:dropdownlist>
                       </td>
                       <td>
                           <asp:label text="Month" runat="server" cssclass="Label"></asp:label>
                       </td>
                       <td>
                           <asp:textbox id="txt_Month" runat="server" readonly="true"> </asp:textbox>
                       </td>
                   </tr>
                   <tr>
                       <td>
                           <asp:label text="Bill No." runat="server" cssclass="Label"></asp:label>
                       </td>
                       <td>
                           <%--<editable:editabledropdownlist id="ddl_Bill" runat="server" autopostback="true" xmlns:editable="#unknown">
                               onselectedindexchanged="ddl_Bill_SelectedIndexChanged1">
                           </editable:editabledropdownlist>--%>
                           <<asp:DropDownList ID="ddl_Bill" runat="server" AutoPostBack="true" EnableViewState="true"
                               onselectedindexchanged="ddl_Bill_SelectedIndexChanged">

                       </td>
                       <td>
                           <asp:label text="Bank" runat="server" cssclass="Label"></asp:label>
                       </td>
                       <td>
                           <asp:dropdownlist id="ddl_Bank" runat="server">
                           </asp:dropdownlist>
                       </td>
                   </tr>
                   <tr>
                       <td>
                           <asp:label text="A/C No." runat="server" cssclass="Label"></asp:label>
                       </td>
                       <td>
                       <asp:textbox id="txt_AccountNo" runat="server" cssclass="textbox"></asp:textbox>
                          <%-- <asp:dropdownlist id="ddl_ACCNo" runat="server">
                           </asp:dropdownlist>--%>
                       </td>
                       <td>
                           <asp:label text="Date Of Check" runat="server" cssclass="Label"></asp:label>
                       </td>
                       <td>
                           <asp:textbox id="txt_dateCheck" runat="server" class="datepicker"></asp:textbox>
                       </td>
                   </tr>
                   <tr>
                       <td>
                           <asp:label text="Check No." runat="server" cssclass="Label"></asp:label>
                       </td>
                       <td>
                           <asp:textbox id="txt_Check_No" runat="server"></asp:textbox>
                       </td>
                       <td>
                           <asp:label id="Label1" text="Total Amount" runat="server" cssclass="Label"></asp:label>
                       </td>
                       <td>
                           <asp:textbox id="txt_Amount" runat="server"></asp:textbox>
                       </td>
                   </tr>
                   <tr>
                       <td>
                           <asp:label text="Enter Amount in Words" runat="server" cssclass="Label"></asp:label>
                       </td>
                       <td>
                           <asp:textbox id="txt_AmountWord" runat="server"></asp:textbox>
                       </td>
                   </tr>
               </table>
               <br />
               <center>
                   <asp:label id="lbl_msg" runat="server" cssclass="Label"></asp:label>
                   <br />
                   <asp:button id="btn_Report" cssclass="myButton" text="Show Report" runat="server">
                       OnClick="btn_Report_Click" />
               </asp:button></center>
               <div style="float: right; margin: -35px 0 0 0;">
                   <asp:button id="btn_Excel" text="Export to Excel" runat="server" cssclass="myButton" />
                   <asp:button id="btn_Word" text="Export to Word" runat="server" cssclass="myButton" />
               </div>
               <br />
           </div>
       </contenttemplate>
   </asp:updatepanel>
 
Share this answer
 
Comments
Member 12048151 24-Oct-15 7:50am    
display
output

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900