I have an asp dropdown for cities, followed by a dropdown for buildings within the selected city, followed by a listview of rooms in the selected building:
<asp:UpdatePanel ID="CityListUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="CityListDropDown" class="form-control" Width="250" runat="server" DataSourceID="CityListDataSource" DataTextField="name" DataValueField="id" AutoPostBack="True" OnSelectedIndexChanged="CityListDropDown_SelectedIndexChanged">
</asp:DropDownList>
<asp:ObjectDataSource ID="CityListDataSource" runat="server" SelectMethod="GetListOfCities" TypeName="RoomTraq.CityDataServiceReference.CityDataServiceClient"></asp:ObjectDataSource>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="BuildingListUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="BuildingListDropDown" class="form-control" Width="250" runat="server" DataSourceID="BuildingListDataSource" DataTextField="name" DataValueField="id" AutoPostBack="True" OnSelectedIndexChanged="BuildingListDropDown_SelectedIndexChanged">
</asp:DropDownList>
<asp:ObjectDataSource ID="BuildingListDataSource" runat="server" SelectMethod="BuildingsByCity" TypeName="RoomTraq.BuildingDataServiceReference.BuildingDataServiceClient">
<SelectParameters>
<asp:ControlParameter ControlID="CityListDropDown" Name="CityId" PropertyName="SelectedValue" Type="Int64" />
</SelectParameters>
</asp:ObjectDataSource>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="RoomListUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:ListView ID="ListView1" runat="server" DataSourceID="RoomListDataSource" InsertItemPosition="LastItem">
....
<asp:ObjectDataSource ID="RoomListDataSource" runat="server" DataObjectTypeName="RoomTraq.RoomDataServiceReference.Room" DeleteMethod="DeleteRoom" InsertMethod="AddNewRoom" SelectMethod="RoomsByBuilding" TypeName="RoomTraq.RoomDataServiceReference.RoomDataServiceClient" UpdateMethod="UpdateRoom">
<SelectParameters>
<asp:ControlParameter ControlID="BuildingListDropDown" Name="BuildingId" PropertyName="SelectedValue" Type="Int64" />
</SelectParameters>
</asp:ObjectDataSource>
As you can see, each element gets its data from a datasource that uses the previous element's selected value as the parameter for the query.
In the code behind, I have
protected void CityListDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
BuildingListUpdatePanel.Update();
RoomListUpdatePanel.Update();
}
protected void BuildingListDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
RoomListUpdatePanel.Update();
}
When the page first comes up, everything displays properly with the first city selected and the list of buildings for that city, and the list of rooms for the first building.
When I choose a different building, the list of rooms updates properly.
PROBLEM: When I choose a different city, the list of buildings updates properly, but the list of rooms does not get updated. Both should update - I have them in update panels so I can do a partial page update of just those panels, and I am telling the panels to update, but the panel does not update itself. The web service that I have serving out the room data never gets called, until I choose a different building. But that leaves my page in a bad state when the building list has updated to reflect the new city selection, but the room list is still showing the rooms for a building in the previous city.
Help Please!!
Related
My FormView wont DataBind. I get no errors, all the elements are found correctly, when I step through the code everything looks like it works as expecting. The select parameter is set and the FormView is DataBound.
But no data is returned and my database logging shows that the procedure that is meant to be DataBound is never touched.
Update Panel
<asp:updatepanel ID="upnlMixingTankInfo" runat="server">
<ContentTemplate>
<asp:formview id="fvMixingTankInfo" runat="server" datasourceid="SqlDataSourceMixingTankInfo">
<ItemTemplate>
<asp:label runat="server">Vessel Capacity:</asp:label>
<asp:TextBox ID="vesselCapacity" runat="server" class="form-control" Text='<%# Bind("fldVesselCapacity")%>'></asp:TextBox>
</ItemTemplate>
</asp:formview>
</ContentTemplate>
Code Behind:
SourceDropDownList = sender
upnlMixingTankInfo = CType(SourceDropDownList.Parent.FindControl("upnlMixingTankInfo"), UpdatePanel)
fvTankInfo = CTYPE(upnlMixingTankInfo.FindControl("fvMixingTankInfo"), FormView)
If Not IsNothing(SourceDropDownList.SelectedValue) Then
SqlDataSourceMixingTankInfo.SelectParameters.Add("TankName", DropDownListEquipmentList.SelectedValue)
End If
fvTankInfo.Databind()
SQLDATSOURCE:
<asp:SqlDataSource ID="SqlDataSourceMixingTankInfo" runat="server"
ConnectionString="<%$ ConnectionStrings:ZMConnectionString %>"
SelectCommand="EXEC stpWebGetMixTankCapacity #TankName" >
<SelectParameters>
<asp:Parameter Name="TankName" defaultvalue=""/>
</SelectParameters>
Few changes in SqlDataSource and done, I've tested using my own sp, modify it according to your usage.
UpdatePanel & FormView
<asp:UpdatePanel ID="upnlMixingTankInfo" runat="server">
<ContentTemplate>
<asp:FormView ID="fvMixingTankInfo" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Label ID="Label1" runat="server">Vessel Capacity:</asp:Label>
<asp:TextBox ID="vesselCapacity" runat="server" class="form-control" Text='<%# Bind("Name")%>'></asp:TextBox>
</ItemTemplate>
</asp:FormView>
</ContentTemplate>
</asp:UpdatePanel>
Code Behind, I've tested on load, you can use same code wherever you want.
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters.Add("Id", "10");
}
SqlDataSource with changes:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="GetImages" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
It works perfectly, I've tested before submitting here.
I have dropdownlist in a GridView (Actually have a bunch) but I am only concerned about one of them. I need to capture when the user makes a new selection to see if they want to add a item into the dropdownlist. Here is my code ..
This never gets called:
protected void DebtorDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.Rows[GridView1.EditIndex];
DropDownList list = (DropDownList)row.FindControl("DebtorDropDownList");
string temp = list.SelectedValue;
}
Here is the ASP code:
<ItemTemplate>
<asp:DropDownList ID="DebtorDropDownList" AppendDataBoundItems="true" runat="server"
DataSourceID="SqlDataSource4" DataTextField="FirstName" DataValueField="contactkey"
SelectedIndexChanged="DebtorDropDownList_SelectedIndexChanged" AutoPostBack="True" >
<asp:ListItem Selected="True">Select</asp:ListItem>
<asp:ListItem >Add New Contact</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:AuditDevConnectionString2 %>"
SelectCommand="sp_fc_vm_getSpokeTo" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" DefaultValue="0" Name="DebtorKey"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
What am I missing? Also am using the right code to find the correct control?
I found my problem. If you look at my ASP closely you will notice that the "SelectedIndexChanged" event is missing "on"
I've two SqlDataSources and two Repeaters, each repeater contains one hyperlink (i also tried using web server button and anchors).
The hyperlinks fetch from the database some values and in the NavigationUrl property I use a string.Format method to create a parameterized url, to pass for the browser, then second repeater is populated according to the value passed in the url which is originally passed by the first repeater's hyperlink
this is my sample code : https://gist.github.com/726213
<asp:ScriptManager id="Scrptmanagr" runat="server"></asp:ScriptManager>
<asp:UpdatePanel id="updtpanl" runat="server">
<ContentTemplate>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [arrange_by_id], [arrange_by] FROM [arrange_by]">
</asp:SqlDataSource>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:HyperLink ID="HyperLink3" NavigateUrl='<%# string.Format("{0}?SortingType={1}",Request.AppRelativeCurrentExecutionFilePath, Eval("arrange_by_id"))%>' runat="server"><%# Eval("arrange_by") %></asp:HyperLink>
</ItemTemplate>
<SeparatorTemplate>
|
</SeparatorTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [alphabet_id],[arrange_by_id], [value] FROM [alphabet] WHERE ([arrange_by_id] = #arrange_by_id)">
<SelectParameters>
<asp:QueryStringParameter Name="arrange_by_id" QueryStringField="SortingType" Type="Int32" DefaultValue="1" />
</SelectParameters>
</asp:SqlDataSource>
<br /><br />
<asp:Repeater ID="Repeater2" runat="server" DataSourceID="SqlDataSource2">
<ItemTemplate>
<asp:HyperLink ID="hyper1" runat="server" NavigateUrl='<%#string.Format("{0}?SortingType={1}&SortBy={2}",Request.AppRelativeCurrentExecutionFilePath, Eval("arrange_by_id"),Eval("value"))%>'><%# Eval("value")%></asp:HyperLink>
</ItemTemplate>
<SeparatorTemplate>
|
</SeparatorTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
Now! everytime I click any of the hyperlinks it causes a full post back and refreshes the page! Am I missing something ?
Pretty sure an <asp:HyperLink> isn't going to get you a partial update, it renders to HTML as an <a href=".."> tag. You'll need a control that actually causes a postback, an <asp:Button> or <asp:LinkButton>.
Firstly any value you want to pass back don't use query strings, its the same page.
Place the content in hidden field or the buttons CommandArgument
<asp:HiddenField ID="hdnFieldName" Value='<%# Eval("columnName") %>' runat="server" />
then on the comand behind
protected void rptName_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if(e.CommandName.Equals("ButtonCommandName"))
{
RepeaterItem objItem = e.Item;
var objFieldValue = (HiddenField)objItem.FindControl("hdnFieldName");
}
}
And remember to set the update panel Mode="conditional' this will cause the updatepanel to only update when one of the following occurs:
1) If a control within the updatepanel causes a postback e.g. asp.net button.
2) If a trigger on the updatepanel occurs (about triggers: http://www.asp.net/web-forms/tutorials/aspnet-ajax/understanding-asp-net-ajax-updatepanel-triggers)
3) Finally if the "Update()" method is called
Otherwise, it won't update and refresh. When its set to always, any postback outside the updatepanel or another updatepanel can trigger the updatepanel to refresh.
I have an asp.net intranet site hosted on IIS on my computer.
On one page you can enter two user id's and get a side by side comparison of their roles in our (custom app) system.
I often use it to add roles when I'm setting up new users. After running the query, if i change a role and run the query again it will not show updated results. It's being cached somehow. I have to go to another page and come back and them run the query to get updated results.
How can avoid having to navigate away to get the updated query results displayed.n
here's the code
<asp:Label ID="Label1" runat="server" Text="Username"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Username"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server"
Text="Compare Permissions" />
<br /><br />
<asp:GridView ID="GridView1" runat="server" CssClass="mGrid" DataSourceID="SqlDataSource1">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="WITH
firstPerson AS
(
select username, security_role from user_role
where username=upper(:Username1)
),
secondPerson as
(
select username, security_role from user_role
where username=upper(:UserName2)
)
select firstPerson.username , firstPerson.security_role,secondPerson.username,secondPerson.security_role from firstPerson FULL JOIN secondPerson
on firstPerson.security_role=secondPerson.security_role order by firstPerson.security_role, secondPerson.Security_role">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="Username1"
PropertyName="Text" />
<asp:ControlParameter ControlID="TextBox2" Name="UserName2"
PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
I think the problem is simply that after you add these new values, you are not refreshing your gridview. There aren't any controls that can "automagically realize" the data has changed. Instead, you need to do it manually, by rebinding your data view control to it's (new) data with DataBind().
Check where you are getting your data.
If you have it in an if statement like
if(!IsPostBack)
That means you will only get the data when you first run. When you are navigate away and then back you are loading the page again for the first time.
How to display list of items in list box while choose a particular item in dropdown list.
example: thiru is a developer he done some modules.
in dropdown list has a list of developers while choose thiru in this list i want to display a what are all the modules completed by thiru that can be listed in listbox
I am using Visual Studio 2008,C# with ASP.Net
Thanks
If your data source is just a List or similar, you can simply do the following:
myListBox.DataSource = myDataSource;
myListBox.DataBind();
If your datasource is a class or list of classes, you need to specify which property to display and which to set as the value.
As above:
myListBox.DataSource = myDataSource;
myListBox.DataTextField = "MyPropertyNameOnMyClass"; //This will be displayed
myListBox.DataValueField = "MyValuePropertyOnMyClass";
myListBox.DataBind();
On the listbox databind your items to a datasource and add a parameter with the value from you drop down list.
This is roughly what you need to do.
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" runat="server" DataSourceID="listDataSource"
DataTextField="Field" DataValueField="Field"></asp:ListBox>
<asp:SqlDataSource ID="listDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:XXXX %>"
SelectCommand=
"SELECT [Field] FROM [LisotOfModules] WHERE ([DevID] = #DevID)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="DevID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="ddlDatasource" DataTextField="DevID"
DataValueField="DevID">
</asp:DropDownList>
<asp:SqlDataSource ID="ddlDatasource" runat="server"
ConnectionString="<%$ ConnectionStrings:XXX %>"
SelectCommand="SELECT [DevID] FROM [Developers]">
</asp:SqlDataSource>
</form>