gridview编辑模式不会在单击时触发

时间:2015-04-16 12:45:57

标签: c# asp.net

我想在单击中编辑gridview中的记录。我的gridview放在更新面板中。 有一个搜索文本框,当我在搜索文本框中输入任何内容时," txt_Search_TextChanged"触发事件并在数据库中搜索该文本,并获取该特定记录并绑定到网格。

现在我的问题是:如果我点击网格的编辑按钮而不搜索,则会触发事件。但是如果我搜索任何记录然后我点击编辑按钮,则需要2次点击才能给出结果。我为textbox写了一个脚本,如下所示:

enter code here
                <script>
            function RefreshUpdatePanel() {
            __doPostBack('<%= txt_Search.ClientID %>', '');

     };

下面是aspx代码

<table width="100%">
    <tr>
        <td align="center">
            <div style="border-width: 2px; border-style: solid; border-color: #FDAC4C; width: 95%; border-radius: 10px;">
                <asp:UpdatePanel ID="UpdatePanel3" runat="server">
                    <ContentTemplate>
                        <asp:GridView ID="grd_Master" ShowFooter="false" runat="server" Width="98%" AllowPaging="true" PagerStyle-CssClass="paging"
                            OnPageIndexChanging="grd_Master_PageIndexChanging"
                            PageSize="8" AutoGenerateColumns="False" BorderWidth="0" BorderStyle="Solid" BorderColor="#FDAC4C" OnRowEditing="grd_Master_RowEditing" ShowHeaderWhenEmpty="true">
                            <AlternatingRowStyle BackColor="#FFFFFF" Height="30px" BorderStyle="Dotted" BorderWidth="1"
                                BorderColor="#DDDDDD" />
                            <RowStyle BackColor="#FFF0DD" Height="30px" BorderStyle="Dotted" BorderWidth="1"
                                BorderColor="#DDDDDD" />

                            <Columns>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        Serial No.<br />

                                    </HeaderTemplate>
                                    <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                                    <ItemStyle HorizontalAlign="Right" Width="10%" CssClass="grdrow"></ItemStyle>
                                    <ItemTemplate>
                                        <asp:Label ID="lblSRNO" runat="server" Style="padding-right: 50px" Text='<%#Container.DataItemIndex+1 %>'></asp:Label>
                                        <asp:Label ID="Cooling_Code" runat="server" Visible="false" Text='<% #Bind("Cool_Code")%>'></asp:Label>
                                        <asp:Label ID="lblProdGrpCode" runat="server" Style="padding-left: 100px" Text='<% #Bind("Prod_Grp_Code")%>' Visible="false"></asp:Label>
                                        <asp:Label ID="lblSubProdGrpCode" runat="server" Style="padding-left: 100px" Text='<% #Bind("Sub_Prod_Code")%>' Visible="false"></asp:Label>
                                        <asp:Label ID="lblMakeCode" runat="server" Style="padding-left: 100px" Text='<% #Bind("Make_Code")%>' Visible="false"></asp:Label>
                                        <asp:Label ID="lblRegerCode" runat="server" Style="padding-left: 100px" Text='<% #Bind("Refg_Code")%>' Visible="false"></asp:Label>
                                        <asp:Label ID="lblCompressorCode" runat="server" Style="padding-left: 100px" Text='<% #Bind("Comp_Code")%>' Visible="false"></asp:Label>
                                        <asp:Label ID="lblFilePath" runat="server" Visible="false" Style="padding-left: 100px" Text='<% #Bind("Document_Path")%>'></asp:Label>
                                    </ItemTemplate>
                                    <HeaderStyle CssClass="grdheader" Width="10%" />
                                </asp:TemplateField>

                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        Cooling Name 
                                    </HeaderTemplate>
                                    <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                                    <ItemStyle HorizontalAlign="Left" Width="20%" CssClass="grdrow"></ItemStyle>
                                    <ItemTemplate>
                                        <asp:Label ID="lblCoolingName" runat="server" Style="padding-left: 10px" Text='<% #Bind("Cool_Name")%>'></asp:Label>
                                    </ItemTemplate>
                                    <HeaderStyle CssClass="grdheader" Width="20%" />
                                </asp:TemplateField>

                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        Short Name
                                    </HeaderTemplate>
                                    <HeaderStyle HorizontalAlign="Left" CssClass="grdheader" Width="15%"></HeaderStyle>
                                    <ItemStyle HorizontalAlign="Left" Width="15%" CssClass="grdrow"></ItemStyle>

                                    <ItemTemplate>
                                        <asp:Label ID="lblCoolingShortName" runat="server" Style="padding-left: 10px" Text='<% #Bind("Cool_Short_Name")%>'></asp:Label>
                                    </ItemTemplate>

                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        Cooling Description
                                    </HeaderTemplate>
                                    <ItemStyle HorizontalAlign="left" Width="45%" CssClass="grdrow"></ItemStyle>
                                    <ItemTemplate>
                                        <asp:Label ID="lblCoolingDescr" runat="server" Style="padding-left: 10px" Text='<% #Bind("Cool_Descr")%>'></asp:Label>
                                    </ItemTemplate>
                                    <HeaderStyle HorizontalAlign="Left" CssClass="grdheader" Width="45%"></HeaderStyle>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        Action<br />

                                    </HeaderTemplate>
                                    <HeaderStyle HorizontalAlign="Left" CssClass="grdheader" Width="10%"></HeaderStyle>
                                    <ItemStyle HorizontalAlign="Center" Width="10%" CssClass="grdrow"></ItemStyle>
                                    <ItemTemplate>
                                        <asp:ImageButton ID="imgbtnEdit" runat="server" ImageUrl="~/images/Edit.png" Width="20px" ToolTip="Edit" CommandName="Edit" Height="20px" />
                                        <asp:ImageButton ID="ImageDelete" runat="server" ImageUrl="~/images/trash1.png" ToolTip="Delete" OnClick="ImageDelete_Click" Width="25px" Height="20px" OnClientClick="return confirmDelete()" />
                                    </ItemTemplate>
                                </asp:TemplateField>

                            </Columns>
                            <PagerStyle HorizontalAlign="Left" BorderWidth="0" BorderStyle="None" />

                        </asp:GridView>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="txt_Search" />
                        <asp:PostBackTrigger ControlID="grd_Master" />
                    </Triggers>
                </asp:UpdatePanel>
            </div>
        </td>
    </tr>
</table>

下面是用于划船的cs代码

protected void grd_Master_RowEditing(object sender, GridViewEditEventArgs e)
{
    try
    {
        tab0.Enabled = false;
        tab1.Enabled = true;

        grd_Master.EditIndex = e.NewEditIndex;
        GridViewRow rw = (GridViewRow)grd_Master.Rows[e.NewEditIndex];
        Label CoolingCode = (Label)rw.FindControl("Cooling_Code");
        Label ProdgrpCode = (Label)rw.FindControl("lblProdGrpCode");
        Label SubProdCode = (Label)rw.FindControl("lblSubProdGrpCode");
        Label MakeCode = (Label)rw.FindControl("lblMakeCode");
        Label ReferCode = (Label)rw.FindControl("lblRegerCode");
        Label CompressoreCode = (Label)rw.FindControl("lblCompressorCode");
        Label CoolingName = (Label)rw.FindControl("lblCoolingName");
        Label CoolingName_Short = (Label)rw.FindControl("lblCoolingShortName");
        Label CoolingName_Desc = (Label)rw.FindControl("lblCoolingDescr");
        Label lblFilePath = (Label)rw.FindControl("lblFilePath");




        txtCoolType.Text = CoolingName.Text.Trim();
        txtCoolTypeShortname.Text = CoolingName_Short.Text.Trim();
        txtCoolTypeDescrip.Text = CoolingName_Desc.Text.Trim();

        ddlProdGrp.SelectedValue = ProdgrpCode.Text;
        ddlSubProd.SelectedValue = SubProdCode.Text;
        ddlMake.SelectedValue = MakeCode.Text;
        ddlCompressor.SelectedValue = CompressoreCode.Text;
        ddlRefe.SelectedValue = ReferCode.Text;

        btnsave.Text = "Update";
        ViewState["CoolingCode"] = CoolingCode.Text;
        TabContainer_Item.ActiveTabIndex = 1;

        Session["CoolingCode"] = CoolingCode.Text;
        this.fu1.BindGridDoc();
    }
    catch (Exception ex)
    {

        Master.ErrorMessage(ex.Message);
    }
}

protected void txt_Search_TextChanged(object sender, EventArgs e)
{
    try
    {
        DataSet ds = gn.ExecuteDataset("Search_CoolingType", txt_Search.Text.Trim());

        ViewState["CoolingTypeData"] = ds.Tables[0];
        if (ds.Tables[0].Rows.Count == 0)
        {
            FillEmptyGrid();

        }
        else
        {

            grd_Master.DataSource = ds;
            grd_Master.DataBind();

        }
    }

    catch (Exception ex)
    {
        Master.ErrorMessage(ex.Message);
    }
}

1 个答案:

答案 0 :(得分:0)

您需要确保网格具有焦点。初始点击可能只是将焦点设置在网格上,然后它会在开始编辑时看到第二次点击。

ActiveControl = grd_Master;事件结束时的简单txt_Search_TextChanged应该可以解决问题。