先于其他函数从GridView控件调用OnSelectedIndexChanged [英] call OnSelectedIndexChanged from GridView before other function

查看:278
本文介绍了先于其他函数从GridView控件调用OnSelectedIndexChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two GridViews. The user can select a row from the first GridView and a list based on the GridView ID selected will display.

第一格:

第二个网格:

code为第一个GridView控件:

Code for first GridView:

<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>

背后code:

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");
                }
            }
        }
    }

code为 gridviewParent_SelectedIndexChanged

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 ();");
    }

使用Javascript:

Javascript:

$(".gvCVRTRow").off();
        $(".gvCVRTRow").click(function (e) {
            ShowAddEditCVRT(this, "Edit");
        });

function ShowAddEditCVRT(sender, AddEdit) {
        $("#divCVRTDetails").fadeIn(300); 
}

对不起了很多code,但我想表现到底发生了什么。当我设置code到 INT指数= gv.Columns.HeaderIndex(选择); 它进入javascript函数 ShowAddEditCVRT 第一,它试图显示第二格 divCVRTDetails 。但我需要先 gridviewParent_SelectedIndexChanged 运行方法,因为这种结合从第一格选择的ID第二个网格。

Sorry its a lot of code but I wanted to show exactly what is happening. When I set the code to int index = gv.Columns.HeaderIndex("Select"); it goes into the javascript function ShowAddEditCVRT first, which tries to display the second grid divCVRTDetails. But I need to run the method gridviewParent_SelectedIndexChanged first because this binds the second grid with the ID selected from the first grid.

我可以先绑定第二网格的唯一方法是将code改成这样: INT指数= gv.Columns.HeaderIndex(纲目日);
因此,用户必须点击选择第一个(与电网绑定),然后单击日期的单元格显示网格。

The only way I can get the second grid to bind first is to change the code to this: int index = gv.Columns.HeaderIndex("Checklist Date");. So the user has to click on select first (to bind the grid) then click on the date cell to display the grid.

所以我的问题是,反正是有运行 gridviewParent_SelectedIndexChanged 方法的JavaScript函数被调用之前?

so my question is, is there anyway to run the gridviewParent_SelectedIndexChanged method before the javascript function gets called?

推荐答案

答案,这是我用 FireJavascriptCallback调用的JavaScript(myFunction的();); 然后只要把javascript的code,我需要这个功能。

Answer to this is I called the javascript by using FireJavascriptCallback("myFunction();"); then just put the javascript code I needed into this function.

FireJavascriptCallback 是函数:

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

这篇关于先于其他函数从GridView控件调用OnSelectedIndexChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆