【发布时间】:2014-02-15 20:45:19
【问题描述】:
我在 asp.net 中有两个从数据库生成的下拉列表。当用户从第一个下拉列表中选择一个值时,将根据该值生成第二个下拉列表。
我还有一个带有添加行、删除和保存功能的gridview。在 gridview 的每个单元格中都有文本框,这些文本框是由添加行函数自行生成的。
我想要做的是从下拉列表中添加一个选择值按钮,并将第二个下拉列表的值添加到 gridview 的第一个单元格中。
但是,如果生成了多个 gridview 行,则应出现一个选项,询问应将值插入哪一行。
请问我可以得到一些帮助吗?
下面是 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>
以下是背后的代码。该代码包含添加行、删除和保存功能。该代码还包含填充下拉列表的功能:
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
}
【问题讨论】:
-
我们可以看看一些代码吗?
-
@lauCosma 页面的所有代码都已添加。我希望它可以帮助您理解我的问题
-
那是很多代码。您能否将问题的相关代码提供给我们?
-
@bjb568 对不起,我不应该把所有代码都放进去。我已经删除了大部分代码,但留下了函数名并注释了函数的作用。我要创建的代码后面的最后一个函数。我还留下了生成下拉菜单的功能。所有的 ASP.net 可能都需要。我希望这会更好
-
你为什么不在下拉的 selectedIndex 更改上生成 gridview 而不是单独的按钮单击?
标签: c# asp.net gridview drop-down-menu