Tuesday, 6 August 2013

Not able to select the default value after choosing a value in dropdownlist

Not able to select the default value after choosing a value in dropdownlist

I have 6 dropdownlist.
ddlnumber
ddlpid1
ddlpid2
ddlpid3
ddlpid4
ddlpid5
The user has to select how many ddlpid would he/she would like to use. It
will then appear according to the user's selection. To add on, I'm trying
to stop duplicated data from appearing in another dropdownlist when being
selected by another ddl. therefore i used this method caught
filldropdownlist to rebind the dropdownlist whenever a selection is made
as shown below.
protected override void OnPreRender(EventArgs e)
{
if (this.PoliceIDsSelection == null)
{
Dictionary<string, string> selections = new Dictionary<string,
string>()
{
{"ddlpid1",""}, {"ddlpid2",""}, {"ddlpid3",""},
{"ddlpid4",""}, {"ddlpid5",""}
};
this.PoliceIDsSelection = selections;
}
base.OnPreRender(e);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillddlLocations();
}
}
protected Dictionary<string, string> PoliceIDsSelection
{
get
{
object d = ViewState["PoliceIDsSelection"];
return (d == null ? null : (Dictionary<String, String>)d);
}
set { ViewState["PoliceIDsSelection"] = value; }
}
protected void FillddlLocations()
{
FillDropdown(ddlpid1);
FillDropdown(ddlpid2);
FillDropdown(ddlpid3);
FillDropdown(ddlpid4);
FillDropdown(ddlpid5);
//ddlpid1.Visible = false;
//ddlpid2.Visible = false;
//ddlpid3.Visible = false;
//ddlpid4.Visible = false;
//ddlpid5.Visible = false;
pnlDDL.Visible = true;
}
protected void FillDropdown(DropDownList ddl)
{
using (var connAdd = new SqlConnection("Data Source = localhost;
Initial Catalog = MajorProject; Integrated Security= SSPI"))
{
connAdd.Open();
var sql = "Select policeid from PoliceAccount where status
='available' and handle ='offcase' and postedto='" +
ddllocation.SelectedValue + "'";
using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
{
DataSet ds2 = new DataSet();
cmdAdd.Fill(ds2);
ddl.Items.Clear();
ddl.DataSource = ds2;
ddl.DataTextField = "policeid";
ddl.DataValueField = "policeid";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("Police ID", ""));
ddl.SelectedIndex = 0;
}
}
}
protected void IndexChanged()
{
bFlag = false;//Prevent fireing the code again while changing the
index
foreach (Control c in pnlDDL.Controls)
{
if (c is DropDownList)
{
string myselection = PoliceIDsSelection[c.ID.ToString()];
FillDropdown((DropDownList)c);
foreach (KeyValuePair<string, string> seletions in
PoliceIDsSelection)
{
if (seletions.Value != myselection)
{
ListItem item1 =
((DropDownList)c).Items.FindByValue(seletions.Value);
((DropDownList)c).Items.Remove(item1);
}
}
((DropDownList)c).SelectedIndex =
((DropDownList)c).Items.IndexOf(((DropDownList)c).Items.FindByValue(myselection));
}
}
}
I also used IndexChanged to notify the ddl from any changes made. The
ddlpid below is able to know whenever the policeID is being selected.
protected void ddlpid_SelectedIndexChanged(object sender, EventArgs e)
{
PoliceIDsSelection[((DropDownList)sender).ID] =
((DropDownList)sender).SelectedValue;
if (bFlag) IndexChanged();
}
But i'm not very sure why am i not able to choose the default value of the
dropdownlist when a value was being selected in the dropdownlist. I used
panel to group my 5 dropdownlist in order to make it easier.
<asp:GridView ID="GWCase" runat="server" DataKeyNames="detail,
propertydetail,suspectdetail " BackColor="#CCCCCC" BorderColor="#999999"
BorderStyle="Solid"
BorderWidth="3px" CellPadding="4" CellSpacing="2"
ForeColor="Black" Width="100%" AutoGenerateSelectButton="True"
OnSelectedIndexChanged="GWCase_SelectedIndexChanged"
AutoGenerateColumns="False" AllowPaging="True" PageSize="5"
OnPageIndexChanging="GWCase_PageIndexChanging" AllowSorting="true"
OnSorting="GridView1_Sorting">
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black"
HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True"
ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
<Columns>
<asp:BoundField DataField="memberreportid" HeaderText="MemberReportID"
SortExpression="memberreportid"/>
<asp:BoundField DataField="typeofcrime" HeaderText="Type of Crime"
SortExpression="typeofcrime" />
<asp:BoundField DataField="crdatetime" HeaderText="ReportDateTime"
SortExpression="crdatetime" />
<asp:BoundField DataField="address" HeaderText="Address"
SortExpression="address" />
<asp:BoundField DataField="incidentdate" HeaderText="Incident Date"
SortExpression="incidentdate" />
<asp:BoundField DataField="incidenttime" HeaderText="Incident Time"
SortExpression="incidenttime"/>
<asp:BoundField DataField="property" HeaderText="Property"
SortExpression="Property"/>
<asp:BoundField DataField="victim" HeaderText="Victim"
SortExpression="victim" />
<asp:BoundField DataField="suspect" HeaderText="Suspect"
SortExpression="suspect"/>
<asp:BoundField DataField="detail" HeaderText="detail"
SortExpression="detail" Visible="false"/>
<asp:BoundField DataField="propertydetail" HeaderText="propertydetail"
SortExpression="propertydetail" Visible="false"/>
<asp:BoundField DataField="suspectdetail" HeaderText="suspectdetail"
SortExpression="suspectdetail" Visible="false"/>
I'm only able to select the default value of other ddl when the all of the
ddl has select an ID. So for example if the user select 3 IDs, the
re-binding wont work if the 5th one does not make a selection.
Regards.

No comments:

Post a Comment