0
我綁定下拉列表和GridView中的複選框列表使用templatefield.But既沒有GridView的OnSelectedIndexChanged事件也沒有OnCheckedChanged事件的複選框越來越火。OnSelectedIndexChanged和OnCheckedChange事件沒有觸發
這是aspx文件代碼。
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="username" OnRowEditing="row_edit" OnRowDeleting="row_delete">
<Columns>
<asp:TemplateField HeaderText="Fetch_username">
<ItemTemplate>
<asp:Label ID="labelusername" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "username")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fetch_firstname">
<ItemTemplate>
<asp:Label ID="labelfirstname" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "firstname")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fetch_lastname">
<ItemTemplate>
<asp:Label ID="labellastname" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "lastname")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fetch_address">
<ItemTemplate>
<asp:Label ID="labeladdress" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "address")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="View">View</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Change Color">
<ItemTemplate>
<asp:DropDownList ID="dropdownid" DataSourceID="sqldatasource_id" DataTextField="username"
BackColor="Yellow" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AppendDataBoundItems="True" runat="server" AutoPostBack="True">
<asp:ListItem Text="" Value="" Selected="True" />
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Change Color">
<ItemTemplate>
<asp:CheckBox ID="checkbox" runat="server" AutoPostBack="true" OnCheckedChanged="checked_delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="delete_checkedbox" runat="server" Text="Delete Using Checkbox" OnClick="delete_box" />
<asp:DropDownList ID="ddlist" runat="server" OnSelectedIndexChanged="index_changed" AutoPostBack="True" BackColor="Blue" >
<asp:ListItem Text="1" Value="" Selected="True" />
<asp:ListItem Text="2" Value="" Selected="False" />
<asp:ListItem Text="3" Value="" Selected="False" />
<asp:ListItem Text="4" Value="" Selected="False" />
<asp:ListItem Text="5" Value="" Selected="False" />
</asp:DropDownList>
<div id="hee" runat="server">
<div class="postionform">
<table border=1>
<tr>
<td><asp:Label ID="label" Text="Username:" runat="server" /></td>
<td><asp:TextBox ID="textbox" BackColor="Silver" runat="server" /></td> <br />
</tr>
<tr>
<td><asp:Label ID="label1" Text="Last-name:" runat="server" /></td>
<td><asp:TextBox ID="textbox1" BackColor="Silver" runat="server" /> </td><br />
</tr>
<tr>
<td><asp:Label ID="label2" Text="Last-name:" runat="server" /></td>
<td> <asp:TextBox ID="textbox2" BackColor="Silver" runat="server" /> </td><br />
</tr>
<tr>
<td><asp:Label ID="label3" Text="Address:" runat="server" /></td>
<td><asp:TextBox ID="textbox3" BackColor="Silver" runat="server" /></td> <br />
</tr>
<tr>
<td><asp:Button ID="button_update" runat="server" OnClick="update_click" Text="Update" /></td>
<td><asp:Button ID="button_cancel" runat="server" OnClick="cancel_click" Text="Cancel" /></td>
</tr>
</table>
</div>
</div>
<asp:SqlDataSource ID="sqldatasource_id" runat="server" ConnectionString="<%$ ConnectionStrings:crudconnection %>"
SelectCommand="SELECT [username] FROM [crudtable]"></asp:SqlDataSource>
<br />
<br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="sqldatasource_id" DataTextField="username"
DataValueField="username" OnSelectedIndexChanged="change">
</asp:DropDownList>
<asp:Label ID="label_for" Text="" runat="server" />
<asp:Label ID="checked_label" Text="" runat="server" />
</form>
這是Cs文件代碼。
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.NamingContainer;
int count = 0;
int g = row.RowIndex + 3;
int current_row_index = row.RowIndex;
foreach (GridViewRow gvRow in GridView1.Rows)
{
gvRow.BackColor = Color.White;
if (((DropDownList)gvRow.FindControl("dropdownid")).SelectedIndex != 0)
{
count++;
}
if (gvRow.FindControl("dropdownid") != null && gvRow.RowIndex != current_row_index)
{
((DropDownList)gvRow.FindControl("dropdownid")).SelectedIndex = 0;
}
}
GridView1.Rows[g].BackColor = Color.Red;
if (count == 0)
{
foreach (GridViewRow gvRow in GridView1.Rows)
{
gvRow.BackColor = Color.White;
}
}
}
protected void checked_delete(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridViewRow gr = (GridViewRow)chk.Parent.Parent;
checked_label.Text = GridView1.DataKeys[gr.RowIndex].Value.ToString();
}