I am trying to figure out how to use AJAX to make my webform automatically update. In my project, I have a dropdownlist that has models of cars. When one of them is selected, and a button is clicked, the program fill a gridView with the records received from the SQL statement. I was wondering if there was any way to be able to make this refresh automatically, and if so how? Here's my asp.net code (code-behind is c#).
<form id="form1" runat="server">
<div>
<asp:GridView ID="gdvCars" runat="server" AutoGenerateColumns="False" DataSourceID="carConnection">
<Columns>
<asp:BoundField DataField="VIN" HeaderText="VIN" SortExpression="VIN" />
<asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" />
<asp:BoundField DataField="Year" HeaderText="Year" SortExpression="Year" />
<asp:BoundField DataField="Color" HeaderText="Color" SortExpression="Color" />
<asp:BoundField DataField="MSRP" HeaderText="MSRP" SortExpression="MSRP" />
<asp:BoundField DataField="Price_Sold" HeaderText="Price_Sold" SortExpression="Price_Sold" />
</Columns>
</asp:GridView>
<asp:DropDownList ID="ddlTables" runat="server" OnSelectedIndexChanged="ddlTables_SelectedIndexChanged">
<asp:ListItem>Select All</asp:ListItem>
<asp:ListItem Value="SRXConnection">SRX</asp:ListItem>
<asp:ListItem Value="CTSConnection">CTS</asp:ListItem>
<asp:ListItem Value="CTSVConnection">CTS-V</asp:ListItem>
<asp:ListItem Value="STSConnection">STS</asp:ListItem>
<asp:ListItem Value="CruzeConnection">Cruze</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="carConnection" runat="server" ConnectionString="<%$ ConnectionStrings:newKroegedlConnectionString %>" SelectCommand="SELECT [VIN], [Model], [Year], [Color], [MSRP], [Price Sold] AS Price_Sold FROM [tCar]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SRXConnection" runat="server" ConnectionString="<%$ ConnectionStrings:newKroegedlConnectionString %>" SelectCommand="SELECT [VIN], [Model], [Color], [Year], [MSRP], [Price Sold] AS Price_Sold FROM [tCar] WHERE ([Model] = #Model)">
<SelectParameters>
<asp:Parameter DefaultValue="SRX" Name="Model" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="CTSConnection" runat="server" ConnectionString="<%$ ConnectionStrings:newKroegedlConnectionString %>" SelectCommand="SELECT [VIN], [Model], [Color], [Year], [MSRP], [Price Sold] AS Price_Sold FROM [tCar] WHERE ([Model] = #Model)">
<SelectParameters>
<asp:Parameter DefaultValue="CTS" Name="Model" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="CTSVConnection" runat="server" ConnectionString="<%$ ConnectionStrings:newKroegedlConnectionString %>" SelectCommand="SELECT [VIN], [Model], [Color], [Year], [MSRP], [Price Sold] AS Price_Sold FROM [tCar] WHERE ([Model] = #Model)">
<SelectParameters>
<asp:Parameter DefaultValue="CTS-V" Name="Model" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="STSConnection" runat="server" ConnectionString="<%$ ConnectionStrings:newKroegedlConnectionString %>" SelectCommand="SELECT [VIN], [Model], [Color], [Year], [Price Sold] AS Price_Sold, [MSRP] FROM [tCar] WHERE ([Model] = #Model)">
<SelectParameters>
<asp:Parameter DefaultValue="STS" Name="Model" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="CruzeConnection" runat="server" ConnectionString="<%$ ConnectionStrings:newKroegedlConnectionString %>" SelectCommand="SELECT [VIN], [Model], [Color], [Year], [MSRP], [Price Sold] AS Price_Sold FROM [tCar] WHERE ([Model] = #Model)">
<SelectParameters>
<asp:Parameter DefaultValue="Cruze" Name="Model" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Button ID="btnChangeView" runat="server" Text="Button" OnClick="btnChangeView_Click" />
</div>
</form>
You could put the gridview, dropdownlist and your button inside an update panel. That will generate a callback that will update only those controls.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
YOUR CONTROLS HERE
</ContentTemplate>
</asp:UpdatePanel>
Related
i have two SqlDataSource one of them depends on a return value of the other
how can i do this
<i><asp:SqlDataSource runat="server" ID="SqlDataSource2" ConnectionString='<%$ ConnectionStrings:mainConnectionString %>' SelectCommand="SELECT * FROM [schools]"></asp:SqlDataSource>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:mainConnectionString %>' SelectCommand="SELECT * FROM [School_imgs] WHERE ([School_ID] = #School_ID)">
<SelectParameters>
<asp:Parameter Name="School_ID" Type="Int32"></asp:Parameter>
</SelectParameters>
</asp:SqlDataSource>"</i>
(I found the way it's supposed to be done as shown in my Answer below.
I'm editing my question and deleting the code I initially put here, which was a mess, leaving just the definitions).
Win7, ASP4.5, empty_web_app in VS2013 with C#. Recreated in a very simple project accessing two tables:
1st table "Students"
student_ID
student_name
student_course_ID (is Forign Key)
2nd table "Courses"
course_ID
course_name
In my web page I have DetailView1 showing details of student who's student_ID is taken from txbStudent_ID.
DetailView1 has Edit Delete and New.
When in UPDATE or INSERT mode I need to show the course_name (rather then the course ID) in a drop-down-list and update/insert accordingly.
No code behind.
My answer below is applicable to GRIDVIEW as well.
Gadi
I was so wrong they way I coded my aspx.
So here is the right way to do it, for future beginners like I am now...
(with the help of https://msdn.microsoft.com/en-us/library/ms178294(v=vs.140).aspx)
<asp:Label ID="Label1" runat="server" Text="Student_ID"></asp:Label>
<asp:TextBox ID="txbStudent_ID" runat="server"></asp:TextBox>
<br />
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="student_ID" DataSourceID="SqlDataSource1" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="student_ID" HeaderText="student_ID" InsertVisible="False" ReadOnly="True" SortExpression="student_ID" />
<asp:BoundField DataField="student_name" HeaderText="student_name" SortExpression="student_name" />
<asp:TemplateField HeaderText="student_course_ID" SortExpression="student_course_ID">
<EditItemTemplate>
<asp:DropDownList
ID="DropDownList1"
runat="server"
DataSourceID="SqlDataSource2"
DataTextField="course_name"
DataValueField="course_ID"
SelectedValue='<%# Bind("student_course_ID") %>'>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList
ID="DropDownList2"
runat="server"
DataSourceID="SqlDataSource2"
DataTextField="course_name"
DataValueField="course_ID"
SelectedValue='<%# Bind("student_course_ID") %>'>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label
ID="Label1"
runat="server"
Text='<%# Bind("student_course_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ShowInsertButton="True" ShowDeleteButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:Course_Student_Connection %>"
InsertCommand="INSERT INTO [Students] ([student_name], [student_course_ID]) VALUES (#student_name, #student_course_ID)"
SelectCommand="SELECT * FROM [Students] WHERE ([student_ID] = #student_ID)"
UpdateCommand="UPDATE [Students] SET [student_name] = #student_name, [student_course_ID] = #student_course_ID WHERE [student_ID] = #student_ID"
DeleteCommand="DELETE FROM [Students] WHERE [student_ID] = #student_ID">
<DeleteParameters>
<asp:Parameter Name="student_ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="student_name" Type="String" />
<asp:Parameter Name="student_course_ID" Type="Int32" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="txbStudent_ID" Name="student_ID" PropertyName="Text" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="student_name" Type="String" />
<asp:Parameter Name="student_course_ID" Type="Int32" />
<asp:Parameter Name="student_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource
ID="SqlDataSource2"
runat="server"
ConnectionString="<%$ ConnectionStrings:Course_Student_Connection %>"
SelectCommand="SELECT [course_ID], [course_name] FROM [Courses]">
</asp:SqlDataSource>
I do hope it will save some time for others.
Stackoverflow is THE greatest and by far the best Q&A site on the web!!!
Gadi.
Sorry I cannot upload the screenshot due to the limited number of my reputation since I am a newbie here.
ora_aspnet_user table with column name userid with datatype of Raw(16)
link to
Instruct Table with userid with datatype of Raw (16) - Other columns is ID (Auto Trigger No), Command (Varchar2 = 256)
MY INLINE ASPX CODE:
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource1"
DefaultMode="Insert" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" Visible="False" />
<asp:BoundField DataField="COMMAND" HeaderText="COMMAND" SortExpression="COMMAND" />
<asp:TemplateField HeaderText="USERID">
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2"
DataTextField="USERNAME" DataValueField="USERID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
InsertCommand="INSERT INTO INSTRUCT (COMMAND, USERID) VALUES (:COMMAND, :USERID)">
<InsertParameters>
<asp:Parameter Name="COMMAND" Type="String" />
<asp:Parameter Name="USERID" DbType="Binary" Size="16" />
</InsertParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT USERID, USERNAME FROM ORA_ASPNET_USERS">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
The above code will only insert the command textbox and but will not insert the USERID from the USERID dropdown.
I would really appreciate your help.
Thank you very much.
Replace your datasource <InsertParameter> with this one, it might help you, because I don't have your source code I cannot test it locally, the problem could occur somewhere elese though:
<InsertParameters>
<asp:ControlParameter ControlID="DropDownList1" PropertyName="SelectedValue" Name="USERID" Type="Byte"></asp:ControlParameter>
<asp:ControlParameter ControlID="DropDownList1" PropertyName="SelectedItem.Text" Name="COMMAND" Type="String"></asp:ControlParameter>
</InsertParameters>
There are 3 dropdownlists which are parent-child, after the 3rd dropdownlist is selected, the DetailsView will display result from all 3 dropdownlist selected. first is ServerName, second is Instance, then third is DatabaseName. After select first dropdownlist of servername, then new list of instance value appear on second dropdownlist. When I select anything on second dropdownlist. There a error message that say, "Must declare the scalar variable "#ServerName"". I don't understand what it mean and please help. Here a dropdownlist codes,
<ajaxToolkit:ComboBox ID="ComboBox1" runat="server" AutoCompleteMode="SuggestAppend" AutoPostBack="True" DataSourceID="SqlDataSource4" DataTextField="ServerName" DataValueField="ServerName" DropDownStyle="Simple" MaxLength="0" style="display: inline;">
</ajaxToolkit:ComboBox>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [ServerName] FROM [tblServer]">
</asp:SqlDataSource>
<br />
<br />
<asp:Label ID="Label11" runat="server" Text="Select Instance:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownInstance" runat="server" AutoPostBack="True" DataSourceID="Instance" DataTextField="Instance" DataValueField="Instance">
</asp:DropDownList>
<asp:SqlDataSource ID="Instance" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [Instance] FROM [tblDatabase] WHERE [ServerName] = #ServerName">
<SelectParameters>
<asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<br />
<asp:Label ID="Label10" runat="server" Text="Select Database:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownDatabase" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource3" DataTextField="DatabaseName" DataValueField="DatabaseName">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [DatabaseName] FROM [tblDatabase] WHERE [ServerName] = #ServerName AND [Instance] = #Instance">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownInstance" Name="Instance" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
Then I pass those dropdownlist function to DetailsView that come with SQLDataSource2,
on SelectCommand in SQLDataSource2, I wrote
SelectCommand="SELECT * FROM [tblDatabase] WHERE (([DatabaseName] = #DatabaseName) AND ([Instance] = #Instance) AND ([ServerName] = #ServerName))"
Then after that I add SelectParameter codes,
<SelectParameters>
<asp:ControlParameter ControlID="DropDownDatabase" Name="DatabaseName" PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="DropDownInstance" Name="Instance" PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
In SqlDataSource3, your query takes two parameters: #ServerName and #Instance. But in the select parameters, you only define #Instance. Yes, you defined #ServerName in the previous data source, but not in this one.
I fixed from what Jay metioned recently and there also another issue that I fix it on instance and database dropdownlist codes, where what I fix and it work fine.
<asp:Label ID="Label11" runat="server" Text="Select Instance:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownInstance" runat="server" AutoPostBack="True" DataSourceID="Instance" DataTextField="Instance" DataValueField="Instance">
</asp:DropDownList>
<asp:SqlDataSource ID="Instance" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [Instance] FROM [tblDatabase] WHERE [ServerName] = #ServerName">
<SelectParameters>
<asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<br />
<asp:Label ID="Label10" runat="server" Text="Select Database:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownDatabase" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource3" DataTextField="DatabaseName" DataValueField="DatabaseName">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [DatabaseName] FROM [tblDatabase] WHERE [ServerName] = #ServerName AND [Instance] = #Instance">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownInstance" Name="Instance" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
I have two Drop Down Lists, degrees and programs respectively, i want to generate the program list values on the bases of selected Degree from degree dropdown list. how can i do this, the code is given below:
<asp:DropDownList ID="Degree_DList" runat="server" DataSourceID="Degrees_DropdownList"
DataTextField="DEGREE_NAME" DataValueField="DEGREE_ID" AppendDataBoundItems="True">
</asp:DropDownList>
<asp:SqlDataSource ID="Degrees_DropdownList" runat="server" ConnectionString="<%$ ConnectionStrings:admConnectionString %>" ProviderName="<%$ ConnectionStrings:admConnectionString.ProviderName %>"
SelectCommand="SELECT DEGREE_ID, DEGREE_NAME FROM DEGREE"> </asp:SqlDataSource>
<asp:DropDownList ID="Program_Dlist" runat="server" AppendDataBoundItems="True" DataSourceID="Programs_DropdownList" DataTextField="PROGRAM_NAME" DataValueField="PROGRAM_ID"></asp:DropDownList>
<asp:SqlDataSource ID="Programs_DropdownList" runat="server" ConnectionString="<%$ ConnectionStrings:admConnectionString %>" ProviderName="<%$ ConnectionStrings:admConnectionString.ProviderName %>"
SelectCommand="SELECT PROGRAM_ID, PROGRAM_NAME FROM PROGRAMS WHERE DEGREE_ID = #DEGREE_ID"><SelectParameters>
<asp:ControlParameter Name="DEGREE_ID" ControlID="Degree_DList" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
You have to inform your DataSource you'll provide some parameters in the SELECT:
<asp:SqlDataSource ... >
<SelectParameters>
<asp:Parameter Name="DEGREE_ID" />
</SelectParameters>
</asp:SqlDataSource>
Do not forget to provide that parameters with code:
MyDataSource.SelectParameters["DEGREE_ID"].DefaultValue = "12345";