0

我正在使用模板字段在gridview中绑定下拉列表和复选框列表。但是gridview的OnSelectedIndexChanged事件和复选框的OnCheckedChanged事件都没有触发。

这是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();
    }
4

1 回答 1

1

我解决了。实际上我正在绑定数据gridviewpage_load所以我忘了把code(which binds the data to grid view and placed inside page_load)里面

(!IsPostBack){}.

希望它会帮助某人。谢谢。

于 2012-08-31T05:34:21.763 回答