我要实现的功能是在一个表中插入数据,初始化时只有一行,点击新增按钮表会自动新增一行,可以插入新数据。
现在问题是要在每一行都有两个dropdownlist,这两个dropdownlist是二级联动的。该怎么实现? aspx代码:
<asp:HiddenField ID="HDListNum" runat="server" />
<table id="tableLike" style="border-color: #C1DAD7; border-width: 1px; border-style: solid;"
cellspacing="0">
<tr align="center" style="background-color: #999999">
<td class="td">
喜好类型
</td>
<td class="td">
喜好内容
</td>
<td class="td">
备注
</td>
</tr>
<%
int perToal2 = Convert.ToInt32(this.HDListNum.Value.Trim());
for (int i = 0; i < perToal2; i++)
{
%>
<tr>
<td valign="middle" class="td">
<asp:DropDownList ID="editDDLikesType" runat="server" Width="90px" OnSelectedIndexChanged="LikesType_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem Value="--请选择--">--请选择--</asp:ListItem>
<asp:ListItem Value="烟">烟</asp:ListItem>
<asp:ListItem Value="酒">酒</asp:ListItem>
<asp:ListItem Value="食">食</asp:ListItem>
<asp:ListItem Value="购物">购物</asp:ListItem>
<asp:ListItem Value="兴趣爱好">兴趣爱好</asp:ListItem>
</asp:DropDownList>
</td>
<td valign="middle" class="td">
<asp:DropDownList ID="ddDrink1" runat="server" AutoPostBack="true">
</asp:DropDownList>
</td>
<td valign="middle" class="td">
<input name="namesNotes" type="text" id="txtNotes" runat="server" />
</td>
</tr>
<% } %>
<tr>
<td>
<asp:Button ID="btnAddList" runat="server" Text="新增" OnClick="AddList" />
</td>
</tr>
</table>cs代码: public void AddList(object sender, EventArgs e)
{
int perToal = Convert.ToInt32(this.HDListNum.Value.Trim());
perToal++;
if (perToal > 7)
{
perToal = 7;
}
this.HDListNum.Value = Convert.ToString(perToal);
}
现在问题是要在每一行都有两个dropdownlist,这两个dropdownlist是二级联动的。该怎么实现? aspx代码:
<asp:HiddenField ID="HDListNum" runat="server" />
<table id="tableLike" style="border-color: #C1DAD7; border-width: 1px; border-style: solid;"
cellspacing="0">
<tr align="center" style="background-color: #999999">
<td class="td">
喜好类型
</td>
<td class="td">
喜好内容
</td>
<td class="td">
备注
</td>
</tr>
<%
int perToal2 = Convert.ToInt32(this.HDListNum.Value.Trim());
for (int i = 0; i < perToal2; i++)
{
%>
<tr>
<td valign="middle" class="td">
<asp:DropDownList ID="editDDLikesType" runat="server" Width="90px" OnSelectedIndexChanged="LikesType_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem Value="--请选择--">--请选择--</asp:ListItem>
<asp:ListItem Value="烟">烟</asp:ListItem>
<asp:ListItem Value="酒">酒</asp:ListItem>
<asp:ListItem Value="食">食</asp:ListItem>
<asp:ListItem Value="购物">购物</asp:ListItem>
<asp:ListItem Value="兴趣爱好">兴趣爱好</asp:ListItem>
</asp:DropDownList>
</td>
<td valign="middle" class="td">
<asp:DropDownList ID="ddDrink1" runat="server" AutoPostBack="true">
</asp:DropDownList>
</td>
<td valign="middle" class="td">
<input name="namesNotes" type="text" id="txtNotes" runat="server" />
</td>
</tr>
<% } %>
<tr>
<td>
<asp:Button ID="btnAddList" runat="server" Text="新增" OnClick="AddList" />
</td>
</tr>
</table>cs代码: public void AddList(object sender, EventArgs e)
{
int perToal = Convert.ToInt32(this.HDListNum.Value.Trim());
perToal++;
if (perToal > 7)
{
perToal = 7;
}
this.HDListNum.Value = Convert.ToString(perToal);
}
解决方案 »
- C#如何快速的读写mdb数据库?
- 截取字符窜的问题,请大家帮忙解答,thank
- 使用directory.getcurrentdirectory获取路径是小写的,求助
- 运行以下代码时,出现“方法必须具有返回类型”的错误提示
- C#怎么获取样优酷的视频?
- InvalidArgument=“0”的值对于“index”无效。
- 给winform程序软件添加时效密码
- 始学C#,需要下载安装什么软件及购买什么入门书?
- 水晶报表能不能实现这个稍复杂的报表,请大家帮忙看看
- 关于winform的treeview的问题
- 结构体中包含string数组转byte[]失败,这是为什么?
- show一个窗体时,如何用屏幕坐标来定位窗体的位置呢?
<Columns>
<asp:TemplateField HeaderText="喜好类型">
<ItemTemplate>
<asp:DropDownList ID="editDDLikesType" runat="server" Width="90px" OnSelectedIndexChanged="editDDLikesType_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem Value="--请选择--">--请选择--</asp:ListItem>
<asp:ListItem Value="烟">烟</asp:ListItem>
<asp:ListItem Value="酒">酒</asp:ListItem>
<asp:ListItem Value="食">食</asp:ListItem>
<asp:ListItem Value="购物">购物</asp:ListItem>
<asp:ListItem Value="兴趣爱好">兴趣爱好</asp:ListItem>
</asp:DropDownList>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("str1") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="喜好内容">
<ItemTemplate>
<asp:DropDownList ID="ddDrink1" runat="server">
</asp:DropDownList>
<asp:HiddenField ID="HiddenField2" runat="server" Value='<%# Eval("str2") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="备注">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("str3") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="添加行" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="删除行" OnClick="Button2_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("str1");
dt.Columns.Add("str2");
dt.Columns.Add("str3");
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int row = ((GridViewRow)((Button)sender).NamingContainer).RowIndex; DataTable dt = new DataTable();
dt.Columns.Add("str1");
dt.Columns.Add("str2");
dt.Columns.Add("str3");
foreach (GridViewRow item in this.GridView1.Rows)
{
DataRow dr = dt.NewRow();
dr["str1"] = (item.Cells[0].FindControl("editDDLikesType") as DropDownList).SelectedValue;
dr["str2"] = (item.Cells[1].FindControl("ddDrink1") as DropDownList).SelectedValue;
dr["str3"] = (item.Cells[2].FindControl("TextBox1") as TextBox).Text;
dt.Rows.Add(dr);
}
dt.Rows.InsertAt(dt.NewRow(), row + 1); this.GridView1.DataSource = dt;
this.GridView1.DataBind();
} protected void Button2_Click(object sender, EventArgs e)
{
int row = ((GridViewRow)((Button)sender).NamingContainer).RowIndex; DataTable dt = new DataTable();
dt.Columns.Add("str1");
dt.Columns.Add("str2");
dt.Columns.Add("str3");
foreach (GridViewRow item in this.GridView1.Rows)
{
DataRow dr = dt.NewRow();
dr["str1"] = (item.Cells[0].FindControl("editDDLikesType") as DropDownList).SelectedValue;
dr["str2"] = (item.Cells[1].FindControl("ddDrink1") as DropDownList).SelectedValue;
dr["str3"] = (item.Cells[2].FindControl("TextBox1") as TextBox).Text;
dt.Rows.Add(dr);
}
dt.Rows.RemoveAt(row);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
protected void editDDLikesType_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList dl = sender as DropDownList;
int row = ((GridViewRow)((DropDownList)sender).NamingContainer).RowIndex;
DropDownList dl2 = this.GridView1.Rows[row].Cells[1].FindControl("ddDrink1") as DropDownList;
dl2.Items.Clear();
if (dl.SelectedValue!="--请选择--")
{
dl2.Items.Add(new ListItem(dl.SelectedValue + "1", dl.SelectedValue + "1"));
dl2.Items.Add(new ListItem(dl.SelectedValue + "2", dl.SelectedValue + "2"));
dl2.Items.Add(new ListItem(dl.SelectedValue + "3", dl.SelectedValue + "3"));
dl2.Items.Add(new ListItem(dl.SelectedValue + "4", dl.SelectedValue + "4"));
}
} protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
(e.Row.Cells[0].FindControl("editDDLikesType") as DropDownList).SelectedValue = (e.Row.Cells[0].FindControl("HiddenField1") as HiddenField).Value;
DropDownList dl1 = e.Row.Cells[0].FindControl("editDDLikesType") as DropDownList;
DropDownList dl2 = e.Row.Cells[1].FindControl("ddDrink1") as DropDownList;
if (dl1.SelectedValue != "--请选择--")
{
dl2.Items.Add(new ListItem(dl1.SelectedValue + "1", dl1.SelectedValue + "1"));
dl2.Items.Add(new ListItem(dl1.SelectedValue + "2", dl1.SelectedValue + "2"));
dl2.Items.Add(new ListItem(dl1.SelectedValue + "3", dl1.SelectedValue + "3"));
dl2.Items.Add(new ListItem(dl1.SelectedValue + "4", dl1.SelectedValue + "4"));
}
dl2.SelectedValue = (e.Row.Cells[1].FindControl("HiddenField2") as HiddenField).Value;
}
}