asp.net dropdownlist - asp.net

I have two dropdownlists for a search, the 1st list is for the city and the second is for the area within the selected city. I would like to add a default value in the 2nd dropdownlist that will search ALL of the areas by default unless a specific area is selected from the list that contains the areaID for a specific search.
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="cityName" AutoPostBack="true" DataValueField="cityID">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Cities]"></asp:SqlDataSource>
<asp:DropDownList ID="DropArea" runat="server" DataSourceID="SqlArea"
DataTextField="areaName" DataValueField="areaID">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlArea" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [area] WHERE ([cityID] = #cityID)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" DefaultValue="0" Name="cityID"
PropertyName="SelectedValue" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" Text="Search" onclick="Button1_Click" />

Bind the second from code behind. Create a data table and insert the Select text at the begining and bind the datatable to the DDL.
DataTable dt =
DataRow dr = dt.NewRow()
dr["areaName"] = "All";
dr["SqlArea"] = "All";
tbldata.Rows.InsertAt(dr,0);
DropArea.DataSource = dt;
DropArea.DataBind();

You can try to add for second dropdownlist OnDataBound="DropArea_DataBound" event.
And use in code:
protected void DropArea_DataBound(object sender, EventArgs e)
{
DropArea.Items.Insert(0, new ListItem() {Value = "-1", Text = "ALL"});
}
And then when you handle click event, check:
var value = DropArea.SelectedItem.Value;
if(string.equals(value, '-1')
{
use your logic here
}
Hope it will help with your problem.
Best regards, Dima.

Chris,
I got the idea below from an answer HERE, but basically you want to modify your query to be in the form of the following:
Your Original:
SELECT * FROM [Cities]
Change To:
SELECT City FROM [Cities]
UNION ALL
SELECT 'ALL'
Old question, but you never found an answer.

Related

can't change selection after DDL get deafult value

am having a data based dropdownlist which the default selection is set by user session. it is working fine, but if i try to select a different item on the list after, its jump back to the default. am using visual studio 2015, web form, asp.net
i try selecting default item in 2 way and still the same problem
if (Session["plyer2"] != null) {
string DropDownListSelected = Session["plyer1"].ToString();
DropDownList1.ClearSelection();
DropDownList1.Items.FindByValue(DropDownListSelected).Selected = true;
DropDownList2.SelectedValue = Session["plyer2"].ToString(); }
have been searching google for the last 2 day didn't find help
the DDL is autopostback
the code is in the .aspx.cs file: protected void Page_Load(object sender, EventArgs e)
the databind is in the .aspx file
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource5" DataTextField="forshow" DataValueField="forshow" AppendDataBoundItems="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="1">בחר גארד</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource5" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [forshow] FROM [players] WHERE ([position] = #position)">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="גארד" Name="position" QueryStringField="גארד" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
well its no a real answer to the problem, but since I'm using session to select item, i add
Session.Abandon();
so its cancelling the IF statement next page load and that's good enough for me

can't get checked nodes from RadTreeView in code behind

I have a rad tree view as below:
<telerik:RadTreeView ID="rtreevwParvandehShakhsi" runat="server" CheckBoxes="true" TriStateCheckBoxes="False" DataSourceID="SqlDataSource1" EnableViewState="true"></telerik:RadTreeView>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:PigisConnectionString %>' SelectCommand="SELECT SCT_ParvandehShakhsi.*, ParvandehShakhsi_ID AS Expr1, ParvandehShakhsi_Code AS Expr2, ParvandehShakhsi_Desc AS Expr3, ParvandehShakhsi_ParentID AS Expr4, ParvandehShakhsi_Personel_ID AS Expr5 FROM SCT_ParvandehShakhsi WHERE (ParvandehShakhsi_Personel_ID = #personel_ID)">
<SelectParameters>
<asp:SessionParameter SessionField="user_personel_id" Name="personel_ID"></asp:SessionParameter>
</SelectParameters>
</asp:SqlDataSource>
<asp:Button ID="btnSubmitParvandehShakhsi" runat="server" Text="ثبت" OnClick="btnSubmitParvandehShakhsi_Click" CausesValidation="false" Font-Size="Medium" CssClass="btn green" Height="38px" Width="63px" />
and I want to get the checked nodes in code behind. i use below code but it dont get the checked node for me! where is the problem?
protected void btnSubmitParvandehShakhsi_Click(object sender, EventArgs e)
{
foreach (RadTreeNode tnode in rtreevwParvandehShakhsi.CheckedNodes)
{
RadWindowManager1.RadAlert("node value : " + tnode.Value, 330, 180, "پیام سیستم", null, null);
}
}
Try setting the DataSourceId of the treeview manually on Page_Load if it is not PostBack.
I think the treeview's datasource gets overriden on page load before the the button click event fires leading to CheckedNodes being empty.

How can I bind a gridview to datasource with dynamic WHERE clause?

I am using Visual Basic for ASP. NET
I know how to populate a gridview with data using only code behind, like this
Dim ds as new datasourse
Dim da as new dataadaptor
Dim con as new SqlConnection
Dim cmd as new SqlCommand
...
Cmd = "select ticketID, problem_text from problems where support_engineer = " & Session("Logged_in_user_id")
...
Gridview1.Datasource = ds
Gridview1.Datasources. DataBind
And that works fines, but my question is: how can i drag and drop a gridview control, and using only the wizard to populate the gridview in design time, how can i define the SELECT statement to only select rows that are related to the logged in user id? See the sql statement above, it uses a session variable that i create in Page_Load event, but how can i use the same logic in design mode? (i don't want to modify the datasource control in code behind)
I looked in youtube and google, and this sites, but all results are simply showing me how to populate the gridview with all rows or with a static condition, not a dynamic one like the one i demonestrated.
Any help is highly appreciated
You could try replacing the form tag in the aspx file
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
SelectCommand="SELECT ticketID, problem_text FROM Tabs WHERE (support_engineer = #Param1)">
<SelectParameters>
<asp:SessionParameter Name="Param1" SessionField="Logged_in_user_id" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True"
DataSourceID="SqlDataSource1">
</asp:GridView>
</div>
</form>
I found out the proper way of binding the gridview to a dynamic session variable, without involving code behind:. Simply use the property <SessionParameter> inside the <SelectParameter>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
SelectCommand="SELECT ticketID, problem_text FROM problems
WHERE (support_engineer = #eng_id)
<SelectParameters>
<asp:SessionParameter
Name="eng_id"
SessionField="LoggedInUser"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True"
DataSourceID="SqlDataSource1">
</asp:GridView>

gridview with multi select listbox

HiI have a list box that is bound to a table in a database. it produces a list of companies. The user will then come along and select multiple companies that they want to view information about, and then they hit a selet button which ill display the company information (e.g company name, company site, address e.t.c) in a gridview underneath. however the issue that i am having is that it only displays ONE of the multiple companies selected and its always the top one.
Can someone please shed some light on how i get all the companies to be displayed in the gridview?
i am programming in vb.net
please see source code below
<asp:ListBox ID="ListBox1" runat="server"
DataSourceID="SqlDataSource11" DataTextField="compName"
DataValueField="compDataID" SelectionMode="Multiple" AutoPostBack="True"></asp:ListBox>
<asp:SqlDataSource ID="SqlDataSource11" runat="server"
ConnectionString="<%$ ConnectionStrings:IWSRiskAssessmentConnectionString %>"
SelectCommand="SELECT [compDataID], [compName] FROM [tblCompany] WHERE ([compDataID] <> #compDataID) ORDER BY [compName]">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="compDataID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView
ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="compName" HeaderText="compName"
SortExpression="compName" />
<asp:BoundField DataField="Site Name" HeaderText="Site Name"
SortExpression="Site Name" />
<asp:BoundField DataField="Reference Number" HeaderText="Reference Number"
SortExpression="Reference Number" />
<asp:BoundField DataField="Asset" HeaderText="Asset" ReadOnly="True"
SortExpression="Asset" />
<asp:BoundField DataField="Location" HeaderText="Location"
SortExpression="Location" />
<asp:BoundField DataField="Block" HeaderText="Block" SortExpression="Block" />
<asp:BoundField DataField="Room" HeaderText="Room" SortExpression="Room" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:IWSRiskAssessmentConnectionString %>"
SelectCommand="SELECT tblCompany.compName, tblSite.siteName AS [Site Name], tblSite.siteUPRN AS [Reference Number], tblIncMain.incAsset + ' ' + CAST(tblIncMain.incNumber AS varchar) AS Asset, tblIncMain.incLocation AS Location, tblIncMain.incBlock AS Block, tblIncMain.incRoom AS Room FROM tblIncMain INNER JOIN tblSite ON tblIncMain.incSite = tblSite.siteID INNER JOIN tblCompany ON tblSite.siteCompany = tblCompany.compDataID WHERE (tblIncMain.incActive = 1) AND (tblSite.siteActive = 1) AND (tblIncMain.incRemoved = 0) AND (tblCompany.compDataID = #compDataID) ORDER BY [Site Name], tblIncMain.incAsset, tblIncMain.incNumber">
<SelectParameters>
<asp:ControlParameter ControlID="ListBox1" Name="compDataID"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
this is the source code :-) please take a look and any help is greatly appreciated
May be DataBind code executes on every request or you are not collecting selected items from the listbox.
Sub page_load()
if Not IsPostBack Then
End If
End sub
and to iterate Listbox items,
For Each item in ListBox1.Items
if item.Selected then
End If
Next
Your data source is only selecting 1 record. The ListBox will return the first value when you use a multiple select ListBox as a ControlParameter.
(tblCompany.compDataID = #compDataID)
What you want to do is have something with an IN statement such as
(tblCompany.compDataID in #compData)
You may need to do something in the code behind.
<asp:ListBox ID="ListBox1" runat="server"
AutoPostBack="True"
DataTextField="compName"
DataSourceID="SqlDataSource11"
DataValueField="compDataID"
OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
SelectionMode="Multiple">
</asp:ListBox>
Code behind
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string s = string.Empty;
foreach (ListItem li in ListBox1.Items)
{
if (li.Selected == true)
s += li.Value + ",";
}
if (s != string.Empty)
{
s = s.Substring(0, s.Length - 2); // chop off trailing ,
SqlDataSource2.SelectParameters["compData"].DefaultValue = s;
}
}
NOTE this hasn't been tested but it is one option you can try. Basically you want a IN not a = for your select to get all results.
ok so i added 'in' to my sql statement, however it still only picked up one selection from the list box instead of multiple this is the sql statement:
SELECT tblCompany.compName, tblSite.siteName AS [Site Name], tblSite.siteUPRN AS [Reference Number], tblIncMain.incAsset + " " + CAST(tblIncMain.incNumber AS varchar) AS Asset, tblIncMain.incLocation AS Location, tblIncMain.incBlock AS Block, tblIncMain.incRoom AS Room FROM tblIncMain INNER JOIN tblSite ON tblIncMain.incSite = tblSite.siteID INNER JOIN tblCompany ON tblSite.siteCompany = tblCompany.compDataID WHERE (tblIncMain.incActive = 1) AND (tblSite.siteActive = 1) AND (tblIncMain.incRemoved = 0) AND tblCompany.compDataID in(#compDataID) ORDER BY [Site Name], tblIncMain.incAsset, tblIncMain.incNumber

Passing a variable to a dynamic dropdownlist

I have 2 dropdownlists, one is State and the second is City. I am trying to create it so, when a user clicks the State, the second dropdownlist is populated with City names from a datatable.
Here is the code
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<%--<cc2:CascadingDropDown ID="CascadingDropDown1" runat="server"> </cc2:CascadingDropDown>--%>
<h1>Live Event Search Engine</h1><br />
State: <asp:DropDownList ID="ddlState" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="ST_Code" DataValueField="ST_Code" /><asp:SqlDataSource
ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbConnection %>"
SelectCommand="SELECT [ST_Code] FROM [State]">
</asp:SqlDataSource>
City: <asp:DropDownList ID="ddlCity" runat="server" DataSourceID="SqlDataSource2" DataTextField="RS_City" DataValueField="RS_City" /><asp:SqlDataSource
ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:dbConnection %>"
SelectCommand="web_PublicProgramListbyState" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter Name="State" SessionField="ST_Code" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Button ID="Submit" runat="server" Text="Submit" />
</asp:Content>
and the code behind is
Public Sub ddlState_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlState.SelectedIndexChanged
Me.ddlCity.Items.Clear()
Dim da As New SqlDataAdapter("web_PublicProgramListbyState", New SqlConnection(ConfigurationManager.AppSettings("dbConnection")))
Dim ds As New DataSet
da.Fill(ds, "#State")
da.Dispose()
Me.ddlCity.DataSource = ds.Tables("Products")
Me.ddlCity.DataTextField = "ProductName"
Me.ddlCity.DataValueField = "ProductID"
Me.ddlCity.DataBind()
End Sub
End Class
Looks like you are trying to bring a dataset into this when you already have a SqlDataSource defined. Just modify the parameters of the SqlDataSource and re-bind:
Public Sub ddlState_SelectedIndexChanged(...)
SqlDataSource2.SelectParameters.Clear()
SqlDataSource2.SelectParameters.Add(New Parameter("#State", DbType.String, ddlState.SelectedValue))
ddlCity.DataBind()
End Sub
Edit: Or you can use a ControlParameter referencing ddlState.SelectedValue in SqlDataSource2.SelectParameters as mentioned in another answer. Only trick there is you have to manage your default values carefully so ddlCity only binds when you want it to.
I would change it to a control param and just call the databind. No need to do the fill yourself.
<asp:ControlParameter Name="State" ControlID="ddlState" Type="String" />
and then in the select event just call:
Me.ddlCity.DataBind()
Or if you want to remove the codebehind all together put it in an update panel with a trigger.
I don't see where the ddlState_SelectedIndexChanged event writes the session variable. This is required.

Resources