简体   繁体   English

从用户控件调用Web服务时,asp.net页为空

[英]asp.net page is null when a web service is called from user control

I have a Usercontrol called "DynamicGrid" it's used on various pages for making dynamic grids when datasets are assigned. 我有一个名为“ DynamicGrid”的Usercontrol,在分配数据集时,该控件在各个页面上用于制作动态网格。 Grids have textboxes to edit/insert data. 网格具有用于编辑/插入数据的文本框。 Now i got a requirement that whenever user hits enter at the last textbox of the Grid it should enter a new row to its grid, this functionality was previously achieved by button's click event. 现在我有了一个要求,即只要用户单击网格的最后一个文本框,它都应在网格中输入新行,此功能以前是通过按钮的click事件实现的。 So I hooked up some javascript to the last textbox like this 所以我像这样将JavaScript连到了最后一个文本框

if (gv != null && gv.Rows.Count > 0)
{
    GridViewRow lastRow = gv.Rows[gv.Rows.Count - 1];
    var controls = lastRow.Controls[lastRow.Controls.Count - 1].Controls[0];
    TextBox txtbx = ((TextBox)lastRow.Controls[lastRow.Controls.Count - 1].Controls[0].Controls[0]);

    string script = string.Format(@"
$(document).ready(function () {{
    $('#{0}').bind('keyup', function (e) {{
        {{
            if (e.keyCode === 13) {{
                {{
                    $('#{0}').css('border', '1px dashed red');
                    $.ajax({{
                        type: 'POST',
                        url: '{1}',
                        contentType: 'application/json; charset=utf-8',
                        dataType: 'json',
                        async: false,
                        data: '{{ GridViewID: ""{2}"" }}',
                        success: function (data) {{ 
                            alert(data.d); 
                        }},
                        error: function (xhr) {{
                            alert('An error occurred: ' + xhr.status + ' ' + xhr.statusText + ' ' + xhr.responseText);
                        }}
                    }});
                }}
            }}
        }}
    }});
}});", txtbx.ClientID, "../../../WebService/DynamicGridAddRow.asmx/AddNewRow", gv.ClientID);

    ScriptManager.RegisterClientScriptBlock(this, Page.GetType(), "script", script, true);
}

On my webservice i used delegates to invoke the addrow method previously used on the button click event 在我的Web服务上,我使用委托来调用以前在按钮单击事件上使用的addrow方法。

[WebMethod]
public void AddNewRow(string GridViewID)
{
    wucDynamicGrid dynamicGrid = new App_Controls.wucDynamicGrid();
    AddNewRowToDynamicGrid newRow = new App_Controls.AddNewRowToDynamicGrid(dynamicGrid.addRow);
    newRow(GridViewID);
}

This is the add new method 这是添加新方法

public void addRow(string GridID)
{
    //GridView gv = (GridView)this.Page.FindControl(GridID);
    Page p = HttpContext.Current.Handler as Page;           
    var controls = APP_Common.CommonFunctions.FindControl<GridView>(this.NamingContainer.Controls);
    GridView gv = controls;
    AddNewRowToDynamicGrid(gv);
}

The problem is Page is always null and so are Parent, NamingContainer and HttpContext.Current.Handler properties. 问题是Page始终为null,Parent,NamingContainer和HttpContext.Current.Handler属性也是如此。 I need to find that Dynamically added Grid by the ID so that i can add a new row. 我需要找到通过ID动态添加的网格,以便可以添加新行。 I am stuck for 2 days in this, I know there is a very basic mistake been done here, please guide me 我在此停留了2天,我知道这里做了一个非常基本的错误,请指导我

After some research on this, i decided not to use the web service as the HttpContext.Current.CurrentHandler was a web service handler beacuse this method was invoked by webservice. 经过对此的一些研究,我决定不将Web服务用作HttpContext.Current.CurrentHandler是Web服务处理程序,因为该方法由Webservice调用。 I searched to configure it to use the current page but couldn't do so. 我搜索将其配置为使用当前页面,但无法这样做。 then i used a manual Postback to to do the same functionality checked my Grids for the lastRow's last textbox and put them in session . 然后我使用手动Postback来执行相同的功能,检查了网格中lastRow的最后一个文本框,并将其置于session中。 Now at Page_LoadComplete of my page i've put them on page through ScriptManager.RegisterStartupScript and used __doPostBack to post required data to my method. 现在,在我页面的Page_LoadComplete ,我通过ScriptManager.RegisterStartupScript将它们放在页面上,并使用__doPostBack将所需的数据发布到我的方法中。

if (e.keyCode === 13) {{
    $('#{0}').css('border', '1px dashed green');                
    __doPostBack('{1}', '{2}');             
}}

Added a button so that i can generate a legal partial postback from above code 添加了一个按钮,以便我可以从上述代码生成合法的部分回发

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnHiddenNewRowAdd" EventName="Click" />
    </Triggers>
</asp:UpdatePanel>
<asp:Button ID="btnHiddenNewRowAdd" runat="server" style="display:none;" /> 

And then on the pageload of my Usercontrol called "DynamicGrid" i checked the hidden fields created by the MS ajax __EVENTTARGET , __EVENTARGUMENT and called the addoRow method 然后在我的用户控件“ DynamicGrid”的页面加载中,我检查了由MS ajax __EVENTTARGET__EVENTARGUMENT创建的隐藏字段,并称为addoRow方法

string eventTarget = Request.Form["__EVENTTARGET"];
if (eventTarget != null && eventTarget.Length > 0 )
{
    string parameter = Request.Form["__EVENTARGUMENT"];
    if (parameter != null && parameter.Length > 0)
        addRow(parameter);
}

Now i think it was stupid of me to search for Page when the request was generated from the Web Service, it has nothing to do with the Page LifeCycle so that's why there was no Page , Parent or NamingContainer . 现在,我认为从Web服务生成请求时搜索Page是我的愚蠢之举,它与Page LifeCycle无关,因此这就是为什么没有PageParentNamingContainer I was going for web service beacuse I didn't used __doPostBack before . 我之所以要使用Web服务,是因为之前没有使用__doPostBack

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM