简体   繁体   中英

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. 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. So I hooked up some javascript to the last textbox like this

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

[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. I need to find that Dynamically added Grid by the ID so that i can add a new row. I am stuck for 2 days in this, I know there is a very basic mistake been done here, please guide me

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

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

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 . I was going for web service beacuse I didn't used __doPostBack before .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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