Accessing Controls from another formview inside the same page - asp.net

I have several forms, each with their own DDL, that I'm using inside a page. I have them in different forms because I need different data sources for each DDL. When I press the Submit button, it gives me an error that it can't find the control "ddlCategory". I assume it is because it is in a different form. Here is the markup:
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID"
DataSourceID="AccessDataSource1" DefaultMode="Insert" >
<InsertItemTemplate>
Select a Category:<br />
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="True"
DataSourceID="AccessDataSource1" DataTextField="ORG_NAME"
DataValueField="ID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT * FROM [ORGANIZATIONS]"/>
<br />
<asp:FormView ID="FormView2" runat="server" DataKeyNames="ID"
DataSourceID="AccessDataSource2" DefaultMode="Insert" >
<InsertItemTemplate>
Select an Organization:<br />
<asp:DropDownList ID="ddlOrg" runat="server"
DataSourceID="AccessDataSource2" DataTextField="SectionName"
DataValueField="ID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT ID,SectionName FROM ORG_SECTIONS WHERE OrgID=#OrgID ">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCategory"
PropertyName="SelectedValue"
Name="ID" Type="String"
DefaultValue="" />
</SelectParameters>
</asp:AccessDataSource>
<br />
<asp:FormView ID="FormView3" runat="server" DataKeyNames="ID"
DataSourceID="AccessDataSource3" DefaultMode="Insert" >
<InsertItemTemplate>
Select an Attorney:<br />
<asp:DropDownList ID="ddlAtty" runat="server"
DataSourceID="AccessDataSource3" DataTextField="Expr1" DataValueField="ATTY_ID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
<asp:AccessDataSource ID="AccessDataSource3" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT ATTY_ID, NAME & ' ' & INITIAL & ' ' & LASTNAME AS Expr1 FROM ATTORNEYS ORDER BY NAME & INITIAL & ' ' & LASTNAME">
</asp:AccessDataSource>
Also, if there is a way to do it inside one formview control, I'd like to know that too.

Did it this way:
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT * FROM [ORGANIZATIONS]"/>
<asp:AccessDataSource ID="AccessDataSource3" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT ATTY_ID, NAME & ' ' & INITIAL & ' ' & LASTNAME AS Expr1 FROM ATTORNEYS ORDER BY NAME & INITIAL & ' ' & LASTNAME">
</asp:AccessDataSource>
<br />
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID"
DataSourceID="AccessDataSource1" DefaultMode="Insert" >
<InsertItemTemplate>
Select a Category:<br />
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="True"
DataSourceID="AccessDataSource1" DataTextField="ORG_NAME"
DataValueField="ID">
</asp:DropDownList>
<br />
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT ID,SectionName FROM ORG_SECTIONS WHERE OrgID=#OrgID ">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCategory"
PropertyName="SelectedValue"
Name="ID" Type="String"
DefaultValue="" />
</SelectParameters>
</asp:AccessDataSource>
Select an Organization:<br />
<asp:DropDownList ID="ddlOrg" runat="server"
DataSourceID="AccessDataSource2" DataTextField="SectionName"
DataValueField="ID">
</asp:DropDownList>
<br />
Select an Attorney:<br />
<asp:DropDownList ID="ddlAtty" runat="server"
DataSourceID="AccessDataSource3" DataTextField="Expr1" DataValueField="ATTY_ID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="AddRec" />
And the code behind:
protected void AddRec(object sender, EventArgs e)
{
DropDownList ddlCategory = (DropDownList)FormView1.FindControl("ddlCategory");
DropDownList ddlOrg = (DropDownList)FormView1.FindControl("ddlOrg");
DropDownList ddlAtty = (DropDownList)FormView1.FindControl("ddlAtty");
string constr = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\webvideos.mdb;";
string cmdstr = "INSERT INTO [Org_Sec_Atty] ([OrgID], [SecID], [Atty_ID]) VALUES (?, ?, ?)";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand com = new OleDbCommand(cmdstr, con);
con.Open();
com.Parameters.AddWithValue("#OrgID", ddlCategory.SelectedValue);
com.Parameters.AddWithValue("#SecID", ddlOrg.SelectedValue);
com.Parameters.AddWithValue("#AttyID", ddlAtty.SelectedValue);
com.ExecuteNonQuery();
con.Close();
Response.Redirect("ManageProfAffs.aspx");
}
And done.

Related

Can I access different controls inside other formview controls?

I have several forms, each with their own DDL, that I'm using inside a page. I have them in different forms because I need different data sources for each DDL. When I press the Submit button, it gives me an error that it can't find the control "ddlCategory". I assume it is because it is in a different formview control. Here is the markup:
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID"
DataSourceID="AccessDataSource1" DefaultMode="Insert" >
<InsertItemTemplate>
Select a Category:<br />
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="True"
DataSourceID="AccessDataSource1" DataTextField="ORG_NAME"
DataValueField="ID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT * FROM [ORGANIZATIONS]"/>
<br />
<asp:FormView ID="FormView2" runat="server" DataKeyNames="ID"
DataSourceID="AccessDataSource2" DefaultMode="Insert" >
<InsertItemTemplate>
Select an Organization:<br />
<asp:DropDownList ID="ddlOrg" runat="server"
DataSourceID="AccessDataSource2" DataTextField="SectionName"
DataValueField="ID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT ID,SectionName FROM ORG_SECTIONS WHERE OrgID=#OrgID ">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCategory"
PropertyName="SelectedValue"
Name="ID" Type="String"
DefaultValue="" />
</SelectParameters>
</asp:AccessDataSource>
<br />
<asp:FormView ID="FormView3" runat="server" DataKeyNames="ID"
DataSourceID="AccessDataSource3" DefaultMode="Insert" >
<InsertItemTemplate>
Select an Attorney:<br />
<asp:DropDownList ID="ddlAtty" runat="server"
DataSourceID="AccessDataSource3" DataTextField="Expr1" DataValueField="ATTY_ID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
<asp:AccessDataSource ID="AccessDataSource3" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT ATTY_ID, NAME & ' ' & INITIAL & ' ' & LASTNAME AS Expr1 FROM ATTORNEYS ORDER BY NAME & INITIAL & ' ' & LASTNAME">
</asp:AccessDataSource>
Here is the code behind:
protected void AddRec(object sender, EventArgs e)
{
DropDownList ddlCategory = (DropDownList)FormView1.FindControl("ddlCategory");
DropDownList ddlOrg = (DropDownList)FormView2.FindControl("ddlOrg");
DropDownList ddlAtty = (DropDownList)FormView3.FindControl("ddlAtty");
string constr = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\webvideos.mdb;";
string cmdstr = "INSERT INTO [Org_Sec_Atty] ([OrgID], [SecID], [AttyID]) VALUES (?, ?, ?)";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand com = new OleDbCommand(cmdstr, con);
con.Open();
com.Parameters.AddWithValue("#OrgID", ddlCategory.SelectedValue);
com.Parameters.AddWithValue("#SecID", ddlOrg.SelectedValue);
com.Parameters.AddWithValue("#AttyID", ddlAtty.SelectedValue);
com.ExecuteNonQuery();
con.Close();
Response.Redirect("ManageProfAffs.aspx");
}
Again, it tells me that it can't find the control ddlCategory. I thought about putting them all in one formview, but how would I make different datasources for each of the dropdown lists? I also tried not having any formview controls at all and just using "this.[ID of the ddl]" but that gave me a null reference value. Any ideas on how to make this work?
Here's how I did it. I ended up putting all the DDLs inside one formview (I didn't realize I could specify different datasources for DDLs within a formview that has its own datasource) and putting the datasource that uses the property to use the value from the previous DDL. Works like a charm now.
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT * FROM [ORGANIZATIONS]"/>
<asp:AccessDataSource ID="AccessDataSource3" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT ATTY_ID, NAME & ' ' & INITIAL & ' ' & LASTNAME AS Expr1 FROM ATTORNEYS ORDER BY NAME & INITIAL & ' ' & LASTNAME">
</asp:AccessDataSource>
<br />
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID"
DataSourceID="AccessDataSource1" DefaultMode="Insert" >
<InsertItemTemplate>
Select a Category:<br />
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="True"
DataSourceID="AccessDataSource1" DataTextField="ORG_NAME"
DataValueField="ID">
</asp:DropDownList>
<br />
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT ID,SectionName FROM ORG_SECTIONS WHERE OrgID=#OrgID ">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCategory"
PropertyName="SelectedValue"
Name="ID" Type="String"
DefaultValue="" />
</SelectParameters>
</asp:AccessDataSource>
Select an Organization:<br />
<asp:DropDownList ID="ddlOrg" runat="server"
DataSourceID="AccessDataSource2" DataTextField="SectionName"
DataValueField="ID">
</asp:DropDownList>
<br />
Select an Attorney:<br />
<asp:DropDownList ID="ddlAtty" runat="server"
DataSourceID="AccessDataSource3" DataTextField="Expr1" DataValueField="ATTY_ID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="AddRec" />
And the code behind:
protected void AddRec(object sender, EventArgs e)
{
DropDownList ddlCategory = (DropDownList)FormView1.FindControl("ddlCategory");
DropDownList ddlOrg = (DropDownList)FormView1.FindControl("ddlOrg");
DropDownList ddlAtty = (DropDownList)FormView1.FindControl("ddlAtty");
string constr = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\webvideos.mdb;";
string cmdstr = "INSERT INTO [Org_Sec_Atty] ([OrgID], [SecID], [Atty_ID]) VALUES (?, ?, ?)";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand com = new OleDbCommand(cmdstr, con);
con.Open();
com.Parameters.AddWithValue("#OrgID", ddlCategory.SelectedValue);
com.Parameters.AddWithValue("#SecID", ddlOrg.SelectedValue);
com.Parameters.AddWithValue("#AttyID", ddlAtty.SelectedValue);
com.ExecuteNonQuery();
con.Close();
Response.Redirect("ManageProfAffs.aspx");
}

Cannot Find Control ID in ControlParameter

I am trying to insert values from a textbox but I get an error that it cannot find the controlid in the controlparameter. The TextBox is inside a formview, which is inside a listview. The SqlDataSource is outside the ListView.
My InsertButton_Click Method
protected void InsertButton_Click(object sender, EventArgs e)
{
var ctrl = (Control)sender;
var formview = (FormView)ctrl.NamingContainer;
formview.ChangeMode(FormViewMode.Insert);
SectionListView.DataSource = SectionDataSource;
SectionListView.DataBind();
}
The InsertItem Template
<InsertItemTemplate>
<tr style="">
<td>
<div style="font-size: .8em;">
<asp:FormView ID="SectionFormView" runat="server" DataKeyNames="SectionItemID" DataSourceID="SectionDataSource">
<ItemTemplate>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" OnClick="InsertButton_Click" Font-Size="1.2em" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" Font-Size="1.2em" />
<asp:Label ID="SectionItemLabel" runat="server" Text="SectionItem" Font-Bold="true" Font-Size="1.2em" />
<asp:TextBox ID="SectionItemTextBox" runat="server" />
<asp:Label ID="SectionItemSubLabel" runat="server" Text="SectionItem Label" Font-Bold="true" Font-Size="1.2em" />
<asp:TextBox ID="SectionItemLabelTextBox" runat="server"/>
</ItemTemplate>
</asp:FormView>
</div>
</td>
</tr>
</InsertItemTemplate>
My SqlDataSource
<asp:SqlDataSource ID="SectionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" SelectCommand="SELECT DISTINCT SectionItem,SectionItemLabel,SectionItemID FROM Core.SectionItem_Lkup"
InsertCommand="INSERT INTO Core.SectionItem_Lkup(SectionItem, SectionItemLabel) VALUES (#SectionItem, #SectionItemLabel)">
<InsertParameters>
<asp:ControlParameter ControlID="SectionItemTextBox" Name="SectionItem" PropertyName="Text" />
<asp:ControlParameter ControlID="SectionItemLabelTextBox" Name="SectionItemLabel" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>
When you want to use the child controls as control parameters, you need to use the qualified id as control id. Try changing the SQL data source as below
<asp:SqlDataSource ID="SectionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" SelectCommand="SELECT DISTINCT SectionItem,SectionItemLabel,SectionItemID FROM Core.SectionItem_Lkup"
InsertCommand="INSERT INTO Core.SectionItem_Lkup(SectionItem, SectionItemLabel) VALUES (#SectionItem, #SectionItemLabel)">
<InsertParameters>
<asp:ControlParameter ControlID="SectionFormView$SectionItemTextBox" Name="SectionItem" PropertyName="Text" />
<asp:ControlParameter ControlID="SectionFormView$SectionItemLabelTextBox" Name="SectionItemLabel" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>

how to use delete command in grid view for updation

i have 3 web pages. admin,student and teacher. i have used grid view on each of this page.my table is having a column as 'status'.which is having default value 0. I want to update this value to 1 when i click on delete in gridview. I have written following code. But it is not working.pls help.
this is .aspx file code-
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TEMPRUJUConnectionString %>"
SelectCommand="SELECT * FROM [login] WHERE ([username] = #username)"
DeleteCommand="UPDATE [login] SET [status]= 1 WHERE [id]=#id"
InsertCommand="INSERT INTO [login] ([name], [midname], [surname], [username], [password], [contact], [dob], [email], [address], [occupation], [ltype]) VALUES (#name, #midname, #surname, #username, #password, #contact, #dob, #email, #address, #occupation, #ltype)"
UpdateCommand="UPDATE [login] SET [name] = #name, [midname] = #midname, [surname] = #surname, [username] = #username, [password] = #password, [contact] = #contact, [dob] = #dob, [email] = #email, [address] = #address, [occupation] = #occupation, [ltype] = #ltype WHERE [Id] = #Id">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
and this is my code behind-
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=SNEHAL-PC\\SNEHAL1;Initial Catalog=TEMPRUJU;Integrated Security=True");
SqlCommand cmd;
SqlDataReader dr;
con.Open();
GridViewRow row = GridView1.Rows[e.RowIndex];
cmd=new SqlCommand("Update login set status=1 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'",con);
dr = cmd.ExecuteReader();
}
when you have SqlDataSource you have no need to implement GridView1_RowDeleting,
first you must tell to SqlDataSource that where it can find the value of delete command parametr(s), in your case #id... for this, you need to set DataKeyNames property on your GridView by database unique key field name, in your case id...
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" DataSourceID="SqlDataSource1">
then, to set delete command parameter on SqlDataSource you must tell that, find value of #id from SelectedDataKey property, from GridView like this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TEMPRUJUConnectionString %>"
DeleteCommand="UPDATE [login] SET [status]= 1 WHERE [id]=#id">
<DeleteParameters>
<asp:ControlParameter ControlID="GridView1" Name="id" PropertyName="SelectedDataKey" />
</DeleteParameters>
</asp:SqlDataSource>
and finaly, you need to, send a Delete command with your gridView, for this, you can enable deleteing on gridview control to add this <asp:CommandField ShowDeleteButton="True" /> into your grid view markup, or you can convert this field into a TemplateFiled and create your own template with any control _which have_ CommandName property, and set that property (CommandName) to keyword Delete like this :
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" Text="Delete me"></asp:LinkButton>
this is a sample with Template Field:
<asp:GridView ID="GridView2" runat="server" DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserID" SortExpression="UserID">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("UserID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("UserID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age" SortExpression="Age">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Age") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
DeleteCommand="UPDATE Profiles SET Age = Age+1 where id = #id" SelectCommand="SELECT * FROM Profiles">
<DeleteParameters>
<asp:ControlParameter ControlID="GridView1" Name="id" PropertyName="SelectedDataKey" />
</DeleteParameters>
</asp:SqlDataSource>
and this is a sample with just enable deleteing :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="UserID" HeaderText="UserID" SortExpression="UserID" />
<asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
DeleteCommand="UPDATE Profiles SET Age = Age+1 where id = #id" SelectCommand="SELECT * FROM Profiles">
<DeleteParameters>
<asp:ControlParameter ControlID="GridView1" Name="id" PropertyName="SelectedDataKey" />
</DeleteParameters>
</asp:SqlDataSource>
i increment age in my sample by every delete command

UpdatePanel not updating ListBox

I have an UpdatePanel containing a ListBox. Whenever I change selection on from a DropDown I want the list to get updated via an UpdatePanel. However this is not working.
This is my code so far:
Protected Sub drpDepartments_SelectedIndexChanged(sender As Object, e As EventArgs) Handles drpDepartments.SelectedIndexChanged
Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
Dim deptComm As String = "SELECT department_id FROM departments WHERE department_name = #DepartmentName"
Dim deptSQL As New SqlCommand
Dim dr As SqlDataReader = deptSQL.ExecuteReader()
deptSQL = New SqlCommand(deptComm, conn)
deptSQL.Parameters.AddWithValue("#DepartmentName", drpDepartments.SelectedItem.Text)
dr.Read()
If dr.HasRows Then
Dim department_id As Integer = Convert.ToInt32(dr("department_id"))
Session("DepartmentID") = department_id
End If
dr.Close()
conn.Close()
ASP
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:ListBox ID="lstDeptMembers" runat="server" DataSourceID="SqlDataSource4" DataTextField="name" DataValueField="name" Height="176px" Width="204px"></asp:ListBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="drpDepartments" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" SelectCommand="SELECT * FROM users u
INNER JOIN
(
Select x.user_id as userid,x.department_id,y.department_name
from user_Department x
inner join departments y
on x.department_id=y.department_id WHERE x.department_id=#parameter
)
f on u.user_id= f.userid">
<SelectParameters>
<asp:SessionParameter Name="parameter" SessionField="DepartmentID" />
</SelectParameters>
</asp:SqlDataSource>
DropDownList:
<asp:DropDownList ID="drpDepartments" runat="server" DataSourceID="SqlDataSource3" DataTextField="department_name" DataValueField="department_name">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" SelectCommand="SELECT [department_name] FROM [departments]"></asp:SqlDataSource>
Screenshot:
How can I make it that the ListBox updates whenever a new selection is clicked from the DropDown ?
EDIT: Code for the table that contains the ListBox etc.. :
<asp:DropDownList ID="drpDepartments" runat="server" DataSourceID="SqlDataSource3" DataTextField="department_name" DataValueField="department_name">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" SelectCommand="SELECT [department_name] FROM [departments]"></asp:SqlDataSource>
<br />
<br />
<table style="width:100%;">
<tr>
<td style="width: 221px">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:ListBox ID="lstDeptMembers" runat="server" DataSourceID="SqlDataSource4" DataTextField="name" DataValueField="name" Height="176px" Width="204px"></asp:ListBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="drpDepartments" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" SelectCommand="SELECT * FROM users u
INNER JOIN
(
Select x.user_id as userid,x.department_id,y.department_name
from user_Department x
inner join departments y
on x.department_id=y.department_id WHERE x.department_id=#parameter
)
f on u.user_id= f.userid">
<SelectParameters>
<asp:SessionParameter Name="parameter" SessionField="DepartmentID" />
</SelectParameters>
</asp:SqlDataSource>
</td>
<td style="width: 398px">
<asp:TextBox ID="txtuserSearch" runat="server"></asp:TextBox>
<asp:Button ID="btnSearchDeptUser" runat="server" Text="Search" />
<br />
<asp:Label ID="lblAddDeptUser" runat="server" Visible="False"></asp:Label>
</td>
</tr>
<tr>
<td >
<asp:Button ID="btnRmvDeptMem" runat="server" Text="Remove" />
</td>
<td>
<asp:Button ID="btnAddDeptUser" runat="server" Text="Add" />
</td>
</tr>
</table>
Full code for Departments.aspx
If you want to update the UpdatePanel immediately when the user selects an item in the DropDownList you have to set it's AutoPostBack property to "True"(default is false):
<asp:DropDownList ID="drpDepartments" runat="server" DataSourceID="SqlDataSource3"
AutoPostack="True" DataTextField="department_name" DataValueField="department_name">
</asp:DropDownList>
try this...
Change this
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
with
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">

DetailsView update - not returning new values

After editing some fields, clicking on Update doesn't show the new values. I have tried two ways to retrieve the new values as you can see from this shortened version of the ItemUpdating event (and both return the old ones) :-
Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
Dim txtPassword As TextBox = CType(DetailsView1.Rows(1).Cells(1).FindControl("txtPassword"), TextBox)
Struct_Student.password = txtPassword.Text
Dim PasswordValue As String = e.NewValues("password").ToString()
Struct_Student.password = PasswordValue
End Sub
Here is a shortened version of the aspx :-
<asp:Content ID="Content1" ContentPlaceHolderID="cpMainContent" Runat="Server">
<div id="Controls">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DetailsView ID="DetailsView1"
runat="server"
AutoGenerateRows="False"
DataKeyNames="student_id" DataSourceID="SqlDataSource1"
ForeColor="Blue"
BackColor="#FFF7E6"
AutoPostBack="True"
AutoGenerateEditButton = True
AutoGenerateInsertButton = True
OnModeChanging="StudentDetailView_ModeChanging"
Height=163px
Width=327px
style="left: 400px; top: 1px; position: absolute;">
<Fields>
<asp:TemplateField
HeaderText="Password">
<EditItemTemplate>
<asp:TextBox
id="txtPassword"
Text = '<%# Bind ("password") %>'
runat = "server" />
<asp:RequiredFieldValidator
ID = "reqPassword"
ControlToValidate = "txtPassword"
Text = "(required)"
Display = "Dynamic"
runat = "server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label
id="PasswordLabel"
runat="server"
Text = '<%# Eval("password") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField
datafield="emailaddress"
headertext="Email address"
SortExpression="emailaddress"
/>
</Fields>
</asp:DetailsView>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:FCLManager %>" ProviderName="MySql.Data.MySqlClient"
SelectCommand="Select * from tblstudentinfo WHERE centre_id = #CentreID and fullname = #FullName"
<SelectParameters>
<asp:Parameter Name="CentreID" Type="Int16" DefaultValue="0" />
<asp:Parameter Name="FullName" Type="String" DefaultValue="0" />
</SelectParameters>
</asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
</div> <%--Controls div--%>
</asp:Content>

Resources