3

In my ASP.Net app, I have a button that launches a modal. The onClick event fires the C# behind code that launches a modal. I am then calling a data table and populating string variables with values from the data table:

   protected void uxTicketHistoryButton_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "key", "launchModal();", true);

        DataTable ticketHist = _dtMgr.GetContactInfoByName(uxContactDropdownList.SelectedValue);
        string rName = ticketHist.Rows[0]["RequestorName"].ToString();
        string rPhone = ticketHist.Rows[0]["RequestorPhone"].ToString();
        string rPhoneExt = ticketHist.Rows[0]["RequestorPhoneExt"].ToString();
        string rEmail = ticketHist.Rows[0]["RequestorEmail"].ToString(); ;
        string rState = ticketHist.Rows[0]["RequestorState"].ToString();
        string rOrg = ticketHist.Rows[0]["RequestorOrg"].ToString();
    }

What I want to do now is add those variable values to my modal inside the panel.

<asp:Button ID="uxTicketHistoryButton" runat="server" Text="Show Ticket History"  style="color: blue;" OnClick="uxTicketHistoryButton_Click"/>&nbsp;

<!-- ModalPopupExtender-->
    <ajaxToolkit:ModalPopupExtender ID="uxTicketHistoryModal" runat="server" PopupControlID="Panel1" TargetControlID="uxTicketHistoryButton"CancelControlID="btnClose" BackgroundCssClass="modalBackground">
    </ajaxToolkit:ModalPopupExtender>
    <asp:Panel ID="Panel1" runat="server" CssClass="modalPopup" align="center" style = "display:none">                                
        **** Add variable values here ****
        <asp:Button ID="btnClose" runat="server" Text="Close" />
    </asp:Panel> 

How do I add the variables from my .cs file to the modal panel in my .aspx file so that it shows up like this (values in the { } )? enter image description here

UPDATE This issue seems to be related to the modal interfering with the _Click event function of my button. I have posted an additional question to resolve this problem first. Then, I believe one of the solutions below may work. Thanks everybody!

gwydion93
  • 1,681
  • 3
  • 28
  • 59
  • Possible duplicate if I understand the issue correctly. https://stackoverflow.com/questions/7406961/how-to-call-a-variable-in-code-behind-to-aspx-page – Shelby115 Feb 21 '19 at 21:29

2 Answers2

3

Webform:

   <asp:Panel ID="Panel1" runat="server" CssClass="modalPopup" align="center" style = "display:none">                                
            Name: <asp:Literal ID="Name" runat="server" /><br>
            Organization: <asp:Literal ID="Organization" runat="server" /><br>
            etc....
            <asp:Button ID="btnClose" runat="server" Text="Close" />
    </asp:Panel>

Codebehind:

Name.Text = ticketHist.Rows[0]["RequestorName"].ToString(); 
Organization.Text = ticketHist.Rows[0]["RequestorOrg"].ToString()
mxmissile
  • 11,464
  • 3
  • 53
  • 79
  • Hmmm, this looks like it would work, but its not showing up. – gwydion93 Feb 21 '19 at 21:54
  • 1
    What's not showing up exactly? The "variables" or the modal? – mxmissile Feb 21 '19 at 22:10
  • The modal launches and the text (Ex: "Name: ") shows up, but then the space next to the text is blank. I have it set up just like your example: `` – gwydion93 Feb 22 '19 at 13:19
  • Is `uxTicketHistoryButton_Click` firing for sure? Does your `ticketHist` row contain data? Place a breakpoint in `uxTicketHistoryButton_Click` to see the values and if its getting hit. – mxmissile Feb 22 '19 at 14:48
  • Aha, that is the source of the issue. See my above edit. I have added an additional question. It appears that the modal launching interferes with the `_Click` function for the button, which grabs the data. So nothing is getting returned. – gwydion93 Feb 22 '19 at 15:04
  • 1
    Alright, I was able to get this to work. I believe @Joey Phillips solution would work too, but this seemed like the easiest to apply to my current situation. Thanks! – gwydion93 Feb 22 '19 at 17:35
  • His solution would work for Asp.Net MVC and Razor only. From your posted code, you are strictly in Web Forms world. – mxmissile Feb 22 '19 at 17:37
  • Glad you got it figured out though. – mxmissile Feb 22 '19 at 17:38
1

You can use ASP.NET Razor to post your c# variables directly into your html.

For Example

<!-- Single statement block -->
@{ var myMessage = "Hello World"; }

<!-- Inline expression or variable -->
<p>The value of myMessage is: @myMessage</p> 

<!-- Multi-statement block -->
@{
var greeting = "Welcome to our site!";
var weekDay = DateTime.Now.DayOfWeek;
var greetingMessage = greeting + " Here in Huston it is: " + weekDay;
}
<p>The greeting is: @greetingMessage</p>

OR

you could use get and set methods to post your variables to the modal from you .cs .

  public class yourModalPage
    {
        public string RequestorName { get; set; }

    }

Then you can send values to your modal from your .cs with

yourModalPage.RequestorName = "RequestorName";
Joey Phillips
  • 1,543
  • 2
  • 14
  • 22