Reputation: 442
I've started quite recently developing asp.net web forms and I have this situation that I just can't figure it out on my own.
I have a linkbutton for which, when clicked, I would like to open a modal popup window. The modal popup contains a panel with a gridview in it, that I would like to bind (fill) before the modal popup is shown based on C# method.
Basically, what I need to do is, when the user clicks the button,
(1) the gridview inside the modal popup gets populated
(2) the modal popup will be displayed showing the user a fully populated gridview.
Here are code snippets:
<%-- Relationship Type--%>
<tr style="background-color:#eee;">
<td class="borderTd">
<asp:LinkButton ID="lnkRelTypes" runat="server" Text="Relationship Type:" OnClick="btPopulateRelTypes_Click" > </asp:LinkButton>
</td>
<td style=" border:0;">
<asp:DropDownList ID="ddlRelationshipType" runat="server" TabIndex="3" CssClass="field"> </asp:DropDownList>
</td>
</tr>
<ajaxToolkit:ModalPopupExtender ID="modalppSearchRelTypes" runat="server" TargetControlID="lnkRelTypes"
PopupControlID="panelMaintainRelTypes" BackgroundCssClass="modalBackground" OkControlID="OkButton"
DropShadow="true" />
The problem is that when I debug it, when I press the linkbutton, the event never gets fired and the modal popup it's displayed, of course, with nothing in it because the gridview was never populated.
I have tried creating a javascript function that would call the method (as a webmethod) assigning it OnClientClick event of the button, but by doing that, inside my static web method I can not use any of the page controls, such as the gridview that I am trying to populate.
LATER EDIT providing the JS used to call a WebMethod that would fill the gridview
<script type="text/javascript">
function PopulateRelTypes()
{
PageMethods.GetResult(OnSucess, OnError);
function OnSucess(result)
{ }
function OnError(result) {
alert(result);
}
}
C# code:
[WebMethod]
public static string GetResult()
{
string result = GetRelTypes();
return result;
}
Error message: An object reference is required for the non-static field, method, or property 'GetRelTypes()'
This was the JS function that was assigned on the OnClientClick event of the link button, but it didn't allow me to use any of the page controls (eg: gridview, labels, etc)
LATER later EDIT :)
I think I forgot to mention something important, which is that both the linkbutton and the gridview from the modalpopup are both inside UpdatePanels. Therefore the postback of the page never gets fired when clicking the linkbutton.
The only case where I made this to work was to create a second button, inside the modal popup, that would fill my gridview by calling the GetRelTypes() method (which was changed to a void not a string), but that's not how I want it to be...
Any suggestions, please?
My Solution:
For the link button from the main form - I've added it an OnClientClick event to call a JS function as follows:
LinkButton:
<asp:LinkButton ID="lnkRelTypes" runat="server" Text="Relationship Type:" OnClientClick="PopulateRelTypes();" > </asp:LinkButton>
JS function:
<script type="text/javascript">
function PopulateRelTypes()
{
document.getElementById('<%= btPopupLoad.ClientID %>').click();
}
</script>
The javascript function calls a hidden button that I've placed inside the modal popup. That hidden button has a server side clickevent that populates the gridview as I need to:
Modalpopup hidden button:
<asp:Button ID="btPopupLoad" runat="server" Text="Load"
CssClass="button" onclick="btPopupLoad_Click" style="display:none;"/>
C# method to fetch the data:
protected void btPopupLoad_Click(object sender, EventArgs e)
{
GetRelTypes();
}
Thank god I finally found a solution to this, it was driving me crazy!!! :) I just hope this solution will help others (as me) someday!
Upvotes: 0
Views: 3898
Reputation: 442
For the link button from the main form - I've added it an OnClientClick event to call a JS function as follows:
LinkButton:
<asp:LinkButton ID="lnkRelTypes" runat="server" Text="Relationship Type:" OnClientClick="PopulateRelTypes();" > </asp:LinkButton>
JS function:
<script type="text/javascript">
function PopulateRelTypes()
{
document.getElementById('<%= btPopupLoad.ClientID %>').click();
}
</script>
The javascript function calls a hidden button that I've placed inside the modal popup. That hidden button has a server side clickevent that populates the gridview as I need to:
Modalpopup hidden button:
<asp:Button ID="btPopupLoad" runat="server" Text="Load"
CssClass="button" onclick="btPopupLoad_Click" style="display:none;"/>
C# method to fetch the data:
protected void btPopupLoad_Click(object sender, EventArgs e)
{
GetRelTypes();
}
Upvotes: 1