How to add C# variables to html modal

梦想与她 提交于 2019-12-12 13:27:08

问题


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


来源:https://stackoverflow.com/questions/54816450/how-to-add-c-sharp-variables-to-html-modal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!