I have two dropdown lists in asp.net which are generated from a database. When the user selected a value from the first dropdown list, the second dropdown list will be generated based on this value.
I also have a gridview with has a add row, delete and save function. Within each cell of the gridview are textboxes which are self generated by the add row function.
What I want to do is to have a add selected value button from the dropdown list and for the value of the second dropdown list to be added into the first cell of the gridview.
However, if there are muliple gridview rows generated, then a option should appear asking which row the value should be inserted into.
Please can I get some help on this?
Below is the ASP.net
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="workout">
<div class="exerciseDD">
<fieldset class="exercise">
<legend>Exercise List</legend>
<div style="text-align: justify">
To begin choose the exercise number you wish to add an exercise too. Then use
the dropdown menu to select the exercise you wish to use in your workout.
Finally click add to insert the exercise into the workout.
<br />
<br />
<asp:Label ID="Label3" runat="server" CssClass="bold"
Text="Choose an exercise number:"></asp:Label>
<br />
<asp:DropDownList ID="ExerNo" runat="server">
<asp:ListItem Selected="True">1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:Label ID="MuscleGDD" runat="server" CssClass="bold" Text="Muscle Group:"></asp:Label>
<br />
<asp:DropDownList ID="MuscleDD" runat="server" AutoPostBack="True"
Height="22px" onselectedindexchanged="MuscleDD_SelectedIndexChanged"
Width="100%">
</asp:DropDownList>
<br />
<br />
<asp:Label ID="Label4" runat="server" CssClass="bold" Text="Exercise Name:"></asp:Label>
<br />
<asp:DropDownList ID="ExerciseDD" runat="server" AutoPostBack="True"
Height="22px" onselectedindexchanged="ExerciseDD_SelectedIndexChanged"
Width="100%">
</asp:DropDownList>
<br />
<br />
<asp:Button ID="Add" runat="server" Text="Add Exercise" onclick="Add_Click" />
<br />
<asp:Label ID="Label5" runat="server" CssClass="bold"
Text="Exercise Description:"></asp:Label>
<br />
<asp:Label ID="Exerdes" runat="server"></asp:Label>
</div>
</fieldset>
</div>
<div class="createWO">
<fieldset class="create">
<legend>Create Workout</legend>
<asp:Label ID="LabelUserId" runat="server" Visible="True"></asp:Label>
<br />
<strong>Workout Name:</strong> <asp:TextBox ID="WorkName" runat="server"></asp:TextBox>
<div>
<asp:Label ID="CurrentDate" runat="server" />
<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false" OnRowDeleting="grvWorkout_RowDeleting">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:BoundField DataField="Userid" HeaderText="User ID" Visible="false"/>
<asp:TemplateField HeaderText="Exercise Name">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Set">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Repetition">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Weight">
<ItemTemplate>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row"
onclick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:gridview>
<asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
<br />
<br />
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Below is the Code behind. The code contains the add row, delete and save function. The code also contains the functionality to populate the dropdown lists:
private void SetInitialRow()
{
//Sets the initial row of the gridview
}
private void AddNewRowToGrid()
{
//Adds a new row and placing any data back into the gridview
}
private void SetPreviousData()
{
//temp save of the dat
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable MuscleG = new DataTable();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT [Muscle_id], [MuscleName] FROM [MuscleGroup]", con);
adapter.Fill(MuscleG);
MuscleDD.DataSource = MuscleG;
MuscleDD.DataTextField = "MuscleName";
MuscleDD.DataValueField = "Muscle_id";
MuscleDD.DataBind();
}
//MuscleDD.Items.Insert(0, new ListItem("Select Muscle Name", "0"));
}
if (!Page.IsPostBack)
{
SetInitialRow();
}
CurrentDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
}
protected void MuscleDD_SelectedIndexChanged(object sender, EventArgs e)
{
int muscleid = Convert.ToInt32(MuscleDD.SelectedValue);
DataTable exercises = new DataTable();
using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT [Exercise_id], [ExerciseName] FROM [Exercise] WHERE [Muscle_id] = " + muscleid, con2);
adapter.Fill(exercises);
ExerciseDD.DataSource = exercises;
ExerciseDD.DataTextField = "ExerciseName";
ExerciseDD.DataValueField = "Exercise_id";
ExerciseDD.DataBind();
}
//ExerciseDD.Items.Insert(0, new ListItem("Select Exercise", "0"));
}
protected void ExerciseDD_SelectedIndexChanged(object sender, EventArgs e)
{
int exerciseid = Convert.ToInt32(ExerciseDD.SelectedValue);
using (SqlConnection con3 = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString))
{
con3.Open();
string cmdStr = "SELECT [Exercise_id], [ExerDesc] FROM [ExerciseDescription] WHERE [Exercise_id] = '" + exerciseid +"'";
SqlCommand Des = new SqlCommand(cmdStr, con3);
SqlDataReader reader = Des.ExecuteReader();
reader.Read();
Exerdes.Text = reader["ExerDesc"].ToString();
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
protected void Button1_Click(object sender, EventArgs e)
{
//Saves data in gridview to database
}
//A method that returns a string which calls the connection string from the web.config
private string GetConnectionString()
{
}
//A method that Inserts the records to the database
private void InsertRecords(StringCollection sc)
{
//Inserts the records into the database
}
protected void Gridview1_RowCreated(object sender, GridViewRowEventArgs e)
{
}
private void SetRowData()
{
//Set data in temp table
}
protected void grvWorkout_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//Deletes the row
}
//This function relates to the button I wish to insert the value of the dropdown menu into the gridview.
protected void Add_Click(object sender, EventArgs e)
{
//Add dropdown value
}
Best Answer
Here is a quick & dirty sample I made based on your requirement to get you started.
Aspx
Codebehind