在其他函数之前从GridView调用OnSelectedIndexChanged

时间:2016-02-23 12:36:15

标签: javascript c# jquery asp.net gridview

我有两个GridView。用户可以从第一个GridView中选择一行,并显示基于所选GridView ID的列表。

第一个网格: enter image description here

第二个网格: enter image description here

第一个GridView的代码:

<table>
    <tbody>
        <tr>
            <td colspan="5">
                <textarea id="id1" cols="50" rows="10"></textarea>
                <!-- RGB value boxes !-->
            </td>
            <td>
                R
                <input type=text size=3 maxlength=3 name="r" value="0" onBlur="rgb(this);">
            </td>
            <td>
                G
                <input type=text size=3 maxlength=3 name="g" value="0" onBlur="rgb(this);">
            </td>
            <td>    
                B
                <input type=text size=3 maxlength=3 name="b" value="0" onBlur="rgb(this);">
            </td>

代码背后:

<asp:GridView  style="width:75%"  
                        ID="gvCVRT" 
                        ShowHeaderWhenEmpty="true"
                        CssClass="tblResults" 
                        runat="server" 
                        OnSelectedIndexChanged="gridviewParent_SelectedIndexChanged"   
                        OnRowDataBound="gvCVRT_RowDataBound"                          
                        DataKeyField="ID" 
                        DataKeyNames="ChecklistID"
                        AutoGenerateColumns="false"
                        allowpaging="false"
                        AlternatingRowStyle-BackColor="#EEEEEE">
                        <HeaderStyle CssClass="tblResultsHeader" />
                        <Columns>
                            <asp:BoundField DataField="ChecklistID" HeaderText="ID"  ></asp:BoundField> 
                            <asp:CommandField ShowSelectButton="True" HeaderText="Select" />
                            <asp:BoundField DataField="ChecklistDate" HeaderText="Checklist Date" dataformatstring="{0:dd/MM/yyyy}"></asp:BoundField>
                            <asp:BoundField DataField="User" HeaderText="User" ></asp:BoundField>
                            <asp:BoundField DataField="Note" HeaderText="Note" ></asp:BoundField>
                            <asp:TemplateField HeaderText="Delete" ItemStyle-CssClass="tblRowDelete">
                                <ItemTemplate>
                                    <asp:LinkButton ID="btnDelete"
                                                runat="server" OnClientClick="event.stopPropagation()" OnClick="btnDeleteCVRT_Click"></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

protected void gvCVRT_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { lookupCVRT work = (lookupCVRT)e.Row.DataItem; GridView gv = sender as GridView; string checklistid = work.ChecklistID.ToString(); e.Row.Attributes.Add("ID", "gvCVRT_" + work.ID); LinkButton btnDelete = (LinkButton)e.Row.FindControl("btnDelete"); btnDelete.CommandArgument = checklistid; if (work.ID != null) { int index = gv.Columns.HeaderIndex("Select"); if (index > -1) { e.Row.Cells[index].Attributes.Add("class", "gvCVRTRow"); e.Row.Cells[index].ToolTip = "Click here to Edit Checklist"; e.Row.Cells[index].Attributes.Add("style", "color:blue;cursor:pointer;cursor:hand"); } } } } 的代码:

gridviewParent_SelectedIndexChanged

使用Javascript:

protected void gridviewParent_SelectedIndexChanged(object sender, EventArgs e)
    {
       List<lookupCVRT> workDetails = lookupCVRT.GetChecklistItemsByChecklistID(Company.Current.CompanyID, ParentID.ToString(), gvCVRT.SelectedDataKey.Value.ToString());
        gvCVRTDetails.DataSource = workDetails;
        gvCVRTDetails.DataBind();
        FireJavascriptCallback("setArgAndPostBack ();");
    }

对不起它有很多代码,但我想准确地说明发生了什么。当我将代码设置为$(".gvCVRTRow").off(); $(".gvCVRTRow").click(function (e) { ShowAddEditCVRT(this, "Edit"); }); function ShowAddEditCVRT(sender, AddEdit) { $("#divCVRTDetails").fadeIn(300); } 时,它首先进入javascript函数int index = gv.Columns.HeaderIndex("Select");,它会尝试显示第二个网格ShowAddEditCVRT。但我需要首先运行方法divCVRTDetails,因为这会将第二个网格与从第一个网格中选择的ID绑定。

我可以让第二个网格首先绑定的唯一方法是将代码更改为:gridviewParent_SelectedIndexChanged。 因此,用户必须先单击选择(绑定网格),然后单击日期单元格以显示网格。

所以我的问题是,无论如何在调用javascript函数之前运行int index = gv.Columns.HeaderIndex("Checklist Date");方法吗?

2 个答案:

答案 0 :(得分:0)

有关详细信息,请参阅以下链接。

http://www.codeproject.com/Questions/246366/run-javascript-from-side-server-in-asp-net-and-csh

绑定gridview后,您可以从服务器端添加javascript。

protected void Page_Load(object sender, EventArgs e)    {
    String script = @"<script language=""Javascript"">
                    $(".gvCVRTRow").off();
                    $(".gvCVRTRow").click(function (e) {
                         ShowAddEditCVRT(this, "Edit");
                    });

                    function ShowAddEditCVRT(sender, AddEdit) {
                        $("#divCVRTDetails").fadeIn(300); 
                    }
                 </script>";
    Page.RegisterClientScriptBlock("experiment", script);
    gvCVRT.Attributes.Add("DataBound", "TestJScript()");

我相信这会取代你的FireJavascriptCallback(&#34; setArgAndPostBack();&#34;);

修改 我更改了代码以更好地匹配您提供的内容以及放置代码的位置。我认为它应该在页面加载下。我还更改了gvCVRT.Attributes.Add,以便在gvCVRT为数据绑定时运行javascript。

答案 1 :(得分:0)

对此的回答是我使用FireJavascriptCallback("myFunction();");调用了javascript,然后将我需要的javascript代码放入此函数中。

FireJavascriptCallback是一个函数:

 protected void FireJavascriptCallback(string JSFunctionName)
    {
        System.Web.UI.ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "Callback", JSFunctionName, true);
    }