How to add C# variables to html modal

2019-07-02 02:57发布

问题:

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 { } )?

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!

回答1:

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


回答2:

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