我有个gridview,里面添加了一个dropdownlist和添加删除按钮。
我每次点击添加按钮,之前gridview里面的dropdownlist里选定的项改成初始值了。
怎么让他选定的状态保存?
我dropdownlist里面的项是自己添加的。
点击按钮是往一个DT里面加一行 再从新绑定GV。
我每次点击添加按钮,之前gridview里面的dropdownlist里选定的项改成初始值了。
怎么让他选定的状态保存?
我dropdownlist里面的项是自己添加的。
点击按钮是往一个DT里面加一行 再从新绑定GV。
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
Style="margin-right: 0px" AutoGenerateColumns="False" Width="796px" Height="140px">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" Width="100px" />
<Columns>
<asp:BoundField DataField="Xuhao" HeaderText="序号" />
<asp:TemplateField HeaderText="工号">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Gonghao") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="姓名">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem>张三</asp:ListItem>
<asp:ListItem>李四</asp:ListItem>
<asp:ListItem>王五</asp:ListItem>
<asp:ListItem>赵六</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email账号">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("Email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="密码">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Paswd") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ISP">
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
<asp:ListItem>蟾蜍</asp:ListItem>
<asp:ListItem>化蛇</asp:ListItem>
<asp:ListItem>玄龟</asp:ListItem>
<asp:ListItem>百灵</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:Button ID="Button3" runat="server" Text="增加" OnClick="Button3_Click" />
<asp:Button ID="Button4" runat="server" Text="删除" OnClick="Button4_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<EmptyDataTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Button ID="Button2" runat="server" Text="Button" />
</EmptyDataTemplate>
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindSouse();
}
}
/**
* 数据绑定
* */
public void bindSouse()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Xuhao"));
dt.Columns.Add(new DataColumn("Gonghao"));
dt.Columns.Add(new DataColumn("Email"));
dt.Columns.Add(new DataColumn("Paswd"));
DataRow dr = dt.NewRow();
dr["Xuhao"] = 1;
dr["Gonghao"] = 1;
dr["Email"] = "[email protected]";
dr["Paswd"] = "5684";
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
Session["dt"] = dt;
}
/**
* 增加 删除GridView的行。
* */
protected void Button3_Click(object sender, EventArgs e)
{
int i = GridView1.Rows.Count;
DataTable dt = Session["dt"] as DataTable;
DataRow dr = dt.NewRow();
dr["Xuhao"] = i;
dr["Gonghao"] = i;
dr["Email"] = "[email protected]";
dr["Paswd"] = "5684";
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button4_Click(object sender, EventArgs e)
{
DataTable dt = Session["dt"] as DataTable;
int row = ((GridViewRow)((Button)sender).NamingContainer).RowIndex;
dt.Rows.RemoveAt(row);
GridView1.DataSource = dt;
GridView1.DataBind();
}
/**
* 通过选择Dropdownlist的值来修改checkboxlist里面的值。
*
* */
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int id = ((GridViewRow)(((DropDownList)sender).NamingContainer)).RowIndex;
bool action = false;
string aaa = GridView1.Rows[0].Cells[0].Text; //绑定模板列不能直接取值。
string aaaa = GridView1.Rows[0].Cells[1].FindControl("Label2").ToString(); //取值的问题
Label lable = GridView1.Rows[0].FindControl("Label2") as Label;
string bb = lable.Text;
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (GridView1.Rows[id].Cells[0].Text == CheckBoxList1.Items[i].Value)
{
action = true;
break;
}
}
if (!action)
{
CheckBoxList1.Items.Add(((DropDownList)sender).SelectedValue);
CheckBoxList1.Items[CheckBoxList1.Items.Count - 1].Value = GridView1.Rows[id].Cells[0].Text;
}
else
{
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Value == GridView1.Rows[id].Cells[0].Text)
{
CheckBoxList1.Items[i].Text = ((DropDownList)sender).SelectedValue;
}
}
}
DropDownList ddl1 = GridView1.Rows[id].Cells[2].FindControl("DropDownList1") as DropDownList;
ddl1.Items[ddl1.SelectedIndex].Selected=true;
string sss = ddl1.SelectedValue.ToString();
}