| | |
how to get the datas when the dropdown list has only one value.
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 36
Reputation:
Solved Threads: 0
I have 3 dropdownlist named as dd1,dd2 and dd3.
in dd1 i have datas getting from database.
then in dd2 bound the data by selecting the dd1 datas...
tat coding is
========
like wise dd3 also will be filled. i have set autopostback=true also.
but my problem is when the dd1 has only one data
then dd2 is not filled.because i have written in selectedindexchanged so based on the dd1 value it ll change
but if it has one data then it s not coming.
so pls suggest me for this problem .
Thank you,
in dd1 i have datas getting from database.
then in dd2 bound the data by selecting the dd1 datas...
tat coding is
========
asp Syntax (Toggle Plain Text)
protected void dd1_SelectedIndexChanged(object sender, EventArgs e) { string a = "select distinct a.descriptions,a.sid from swift a,pc_link b where swiftid='loc'and a.sid=b.locationid and blockid="+dd1.SelectedValue; adp = new SqlDataAdapter(a, con); ds = new DataSet(); adp.Fill(ds, "swift"); dd2.DataSource = ds.Tables["swift"].DefaultView; dd2.DataTextField = "descriptions"; dd2.DataValueField = "sid"; dd2.DataBind(); }
like wise dd3 also will be filled. i have set autopostback=true also.
but my problem is when the dd1 has only one data
then dd2 is not filled.because i have written in selectedindexchanged so based on the dd1 value it ll change
but if it has one data then it s not coming.
so pls suggest me for this problem .
Thank you,
Last edited by peter_budo; Mar 27th, 2008 at 4:54 am. Reason: Keep It Organized - please use [code] tags
For your kind information on SelectedIndexChanged events fires only when index of item changes. it wont fires if there is only one item in drop down.
Permant Solution
if you realy want to achive that, please add a dummy item like ["---Select a value--"] in the dropdownbox.
Temp Solution
On Page Load Event.
Please do the following,
Load the dropdown1 with data
Load dropdown2 with default firstitems dropdown1 value.
Load dropdown3 with default firstitems dropdown2 value.
Regards
razool
Permant Solution
if you realy want to achive that, please add a dummy item like ["---Select a value--"] in the dropdownbox.
Temp Solution
On Page Load Event.
Please do the following,
Load the dropdown1 with data
Load dropdown2 with default firstitems dropdown1 value.
Load dropdown3 with default firstitems dropdown2 value.
Regards
razool
•
•
Join Date: Mar 2008
Posts: 36
Reputation:
Solved Threads: 0
•
•
•
•
For your kind information on SelectedIndexChanged events fires only when index of item changes. it wont fires if there is only one item in drop down.
Permant Solution
if you realy want to achive that, please add a dummy item like ["---Select a value--"] in the dropdownbox.
Temp Solution
On Page Load Event.
Please do the following,
Load the dropdown1 with data
Load dropdown2 with default firstitems dropdown1 value.
Load dropdown3 with default firstitems dropdown2 value.
Regards
razool
hi
when i add the <!--select the value--> it show an error so help me to do pls
the error is...........A potentially dangerous Request.Form value was detected from the client (dd3="<!--Select the item-...").
code behind coding
----------------------
asp Syntax (Toggle Plain Text)
protected void Page_Load(object sender, EventArgs e) { con = new SqlConnection("server=SOFTWARE4;uid=sa;pwd=;database=Helpdesk1"); con.Open(); txt_date.Text = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt"); if (!Page.IsPostBack) { loaddata(); } } void loaddata() { string s = "select * from swift where swiftid='blk' and sid in(1,2,3,4,5)"; adp = new SqlDataAdapter(s, con); ds = new DataSet(); adp.Fill(ds, "swift"); dd1.DataSource = ds.Tables["swift"].DefaultView; dd1.DataTextField = "descriptions"; dd1.DataValueField = "sid"; dd1.DataBind(); } protected void Button1_Click(object sender, EventArgs e) { string a = "insert into swiftresult values(" + dd1.SelectedValue + "," + dd2.SelectedValue + "," + dd3.SelectedValue + ","+dd5.SelectedValue+",'" + dd4.SelectedValue + "','" + TextBox1.Text + "','"+txt_date.Text+"','n',null,null,null,null,null)"; SqlCommand cmd = new SqlCommand(a, con); cmd.ExecuteNonQuery(); string b = "select callno from swiftresult where callno=(select max(callno) from swiftresult)"; SqlCommand cmd1 = new SqlCommand(b, con); SqlDataReader dr = cmd1.ExecuteReader(); if (dr.Read()) { Session["callno"] = dr["callno"]; } dr.Close(); Response.Redirect("regist.aspx"); } protected void dd1_SelectedIndexChanged(object sender, EventArgs e) { string a = "select distinct a.descriptions,a.sid from swift a,pc_link b where swiftid='loc'and a.sid=b.locationid and blockid="+dd1.SelectedValue; adp = new SqlDataAdapter(a, con); ds = new DataSet(); adp.Fill(ds, "swift"); dd2.DataSource = ds.Tables["swift"].DefaultView; dd2.DataTextField = "descriptions"; dd2.DataValueField = "sid"; dd2.DataBind(); } protected void dd2_SelectedIndexChanged(object sender, EventArgs e) { string b = "select distinct a.descriptions,b.depid from swift a,pc_link b where a.swiftid='dep' and b.blockid=" + dd1.SelectedValue + " and b.locationid=" + dd2.SelectedValue + " and a.sid=b.depid"; SqlDataAdapter adp1 = new SqlDataAdapter(b, con); DataSet ds = new DataSet(); adp1.Fill(ds); dd3.DataSource = ds; dd3.DataTextField = "descriptions"; dd3.DataValueField ="depid"; dd3.DataBind(); } protected void dd3_SelectedIndexChanged(object sender, EventArgs e) { string k = "select distinct a.descriptions,b.pcid from swift a,pc_link b where a.swiftid='pc' and b.blockid="+dd1.SelectedValue+" and b.locationid="+dd2.SelectedValue+"and a.sid=b.pcid and b.depid="+dd3.SelectedValue; SqlDataAdapter adp3 = new SqlDataAdapter(k, con); DataSet ds = new DataSet(); adp3.Fill(ds); dd5.DataSource = ds; dd5.DataTextField = "descriptions"; dd5.DataValueField = "pcid"; dd5.DataBind(); }
html coding
-------------------
html Syntax (Toggle Plain Text)
<body> <form id="form1" runat="server"> <div> <br /> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HelpdeskConnectionString %> " SelectCommand="SELECT [blockid], [block] FROM [Helpdesk_block]"></asp:SqlDataSource> <asp:TextBox ID="txt_date" runat="server" Visible="False"> </asp:TextBox> <asp:Panel ID="Panel1" runat="server" BackColor="Teal" BorderColor="Teal" Height="7px" Width="482px" Font-Bold="True" Font-Size="X-Large"> <asp:Panel ID="Panel2" runat="server" BackColor="White" Height="371px" Width="563px" BorderColor="Teal" BorderStyle="Solid"> <asp:Label ID="Label2" runat="server" Text="Block" Width="91px" Font-Bold="True" Font-Size="Medium"> </asp:Label> <asp:DropDownList ID="dd1" runat="server" Width="130px" AutoPostBack="True" OnSelectedIndexChanged="dd1_SelectedIndexChanged" Font-Size="Small"> </asp:DropDownList> <asp:Label ID="Label3" runat="server" Text="Location" Width="83px" Font-Bold="True" Font-Size="Medium"> </asp:Label> <asp:DropDownList ID="dd2" runat="server" Width="130px" AutoPostBack="True" OnSelectedIndexChanged="dd2_SelectedIndexChanged" Font-Size="Small"> </asp:DropDownList> <br /> <asp:Label ID="Label4" runat="server" Text="Departments" Width="116px" Font-Bold="True" Font-Size="Medium"> </asp:Label> <asp:DropDownList ID="dd3" runat="server" Width="130px" OnSelectedIndexChanged="dd3_SelectedIndexChanged" AutoPostBack="True" Font-Size="Small"> <asp:ListItem> <!--Select the item--></asp:ListItem> </asp:DropDownList> <br /> <asp:Label ID="Label7" runat="server" Font-Size="Medium" Text="Pc-Name" Width="118px"> </asp:Label> <asp:DropDownList ID="dd5" runat="server" AutoPostBack="True" Width="130px" Font-Size="Small"> </asp:DropDownList> <br /> <br /> <asp:Label ID="Label5" runat="server" Text="Problem Type" Width="115px" Font-Bold="True" Font-Size="Medium"> </asp:Label> <asp:DropDownList ID="dd4" runat="server" AutoPostBack="True" Width="130px" OnSelectedIndexChanged="dd4_SelectedIndexChanged" Font-Size="Small"> <asp:ListItem> Software</asp:ListItem> <asp:ListItem Selected="True"> Hardware</asp:ListItem> <asp:ListItem> Others</asp:ListItem> </asp:DropDownList> <br /> <asp:DropDownList ID="dd6" runat="server" AutoPostBack="True" OnSelectedIndexChanged="dd6_SelectedIndexChanged" Width="130px" Visible="False" Font-Size="Small"> </asp:DropDownList> <br /> <br /> <div style="z-index: 102; left: 245px; width: 113px; position: absolute; top: 361px; height: 35px"> <asp:Button ID="Button1" runat="server" Text="Save" Width="114px" OnClick="Button1_Click" /> </div> <div style="z-index: 101; left: 116px; width: 179px; position: absolute; top: 283px; height: 20px"> <asp:Label ID="Label6" runat="server" Text="Software Definitions" Width="104px" Font-Bold="True" Font-Size="Medium" Visible="False"> </asp:Label> </div> </asp:Panel> </asp:Panel> </div> </div> <div style="z-index: 102; left: 395px; width: 181px; position: absolute; top: 398px; height: 47px"> <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="Medium" Text="Hardware Definitions" Width="109px" Visible="False"> </asp:Label> </div> <div style="z-index: 103; left: 398px; width: 371px; position: absolute; top: 406px; height: 49px"> <asp:Label ID="Label8" runat="server" Font-Size="Medium" Text="Others Definitions" Visible="False" Font-Bold="True"> </asp:Label> <asp:TextBox ID="TextBox1" runat="server" Height="37px" TextMode="MultiLine" Visible="False" Width="185px"> </asp:TextBox> </div> </form> </body>
the coding is
Last edited by peter_budo; Mar 27th, 2008 at 4:55 am. Reason: Keep It Organized - please use [code] tags
For adding the dummy item try this code
C# Syntax (Toggle Plain Text)
ddlRecNature.Items.Insert(0, new ListItem("Select")); ddlRecNature.SelectedIndex = 0;
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
You actually don't even need to set the selected index, since you are inserting it into the default first position. But yes, that would work perfectly:
dd1.Items.Insert(0, new ListItem("-- SELECT HERE --"));
0 is the position, since it is 0 based and not 1, 0 means first position.
dd1.Items.Insert(0, new ListItem("-- SELECT HERE --"));
0 is the position, since it is 0 based and not 1, 0 means first position.
I answer pm's.
I answer questions.
I answer quickly.
I answer.
I answer questions.
I answer quickly.
I answer.
![]() |
Other Threads in the ASP.NET Forum
- Previous Thread: How to show output with my condition from table with vb.net
- Next Thread: how to add Hyperlinks to an asp.net 2.0 page at run time
| Thread Tools | Search this Thread |
.net 2.0 3.5 activexcontrol advice ajax alltypeofvideos asp asp.net bc30451 bottomasp.net browser businesslogiclayer button c# c#gridviewcolumn checkbox child click commonfunctions compatible confirmationcodegeneration content contenttype countryselector courier css dataaccesslayer database datagrid datagridview datagridviewcheckbox datalist deadlock development dgv dropdownlist dropdownmenu edit expose feedback flash flv form formatdecimal forms formview gridview homeedition hosting iframe iis javascript jquery list listbox login menu microsoft mono mouse mssql multistepregistration nameisnotdeclared news numerical objects order panelmasterpagebuttoncontrols radio ratings rotatepage save schoolproject search security serializesmo.table silverlight smartcard sql-server sqlserver2005 suse textbox tracking typeof unauthorized validation vb.net video videos virtualdirectory vista visual-studio visualstudio web webarchitecture webdevelopemnt webservice xml youareanotmemberofthedebuggerusers






