值没有从ascx codebehind传递到javascript?

时间:2021-12-19 09:50:28

I am using this to transfer values from ascx codebehind to ascx user control which has 3 Labels

我使用它将值从ascx codebehind传输到具有3个标签的ascx用户控件

 string text1 = fr.data[0].name;
        string text2 = m.data[0].name;
        string text3 = m.data[0].name;
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), " ", "document.getElementById('lblfriend').innerHTML ='" + text1 + "'; return true;", true);
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), " ", "document.getElementById('lblmyname').innerHTML ='" + text2 + "'; return true;", true);
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), " ", "document.getElementById('lblmyname1').innerHTML ='" + text3 + "'; return true;", true);

However values aren't getting passed and i want to render those Labels as Html so that they can be embedded in an email

但是值不会传递,我想将这些标签呈现为Html,以便它们可以嵌入到电子邮件中

Please help!

3 个解决方案

#1


1  

You could expose the label text as string properties in the codebehind of the UserControl. Then when you render the usercontrol, pass the values to it and it would render the text of the labels.

您可以在UserControl的代码隐藏中将标签文本公开为字符串属性。然后,当您渲染usercontrol时,将值传递给它,它将呈现标签的文本。

Codebehind for the UserControl:

UserControl的Codebehind:

public partial class WebUserControl1 : System.Web.UI.UserControl
{
    private string _Friend;

    public string Friend
    {
        get
        {
            return _Friend;
        }
        set
        {
            _Friend = value;
            lblfriend.Text = value;
        }
    }

    private string _MyName;

    public string MyName
    {
        get
        {
            return _MyName;
        }
        set
        {
            _MyName = value;
            lblmyname.Text = value;
            lblmyname1.Text = value;
        }
    }

}

Then you could load your values as such in your parent control/page:

然后,您可以在父控件/页面中加载您的值:

        string text1 = fr.data[0].name;
        string text2 = m.data[0].name;                    
        MyUserControl.Friend = text1;
        MyUserControl.MyName = text2;

#2


4  

Look at the rendered page, are you sure your controls are named lblFriend and so on, I'm assuming they have a little more to it than that.

看看渲染的页面,你确定你的控件被命名为lblFriend等等,我假设它们还有更多的东西。

You may need to use

您可能需要使用

"document.getElementById('" + lblfriend.ClientID + "').innerHTML ='

#3


0  

Write only one RegisterClientScript with control's ClientID e.g.

只写一个RegisterClientScript和控件的ClientID,例如

String.Format(Page.ClientScript.RegisterClientScriptBlock(this.GetType(), " ", "document.getElementById('{0}').innerHTML ='" + text1 + "'; document.getElementById('{1}').innerHTML ='" + text2 + "';document.getElementById('{2}').innerHTML ='" + text3 + "'; return true;", true),lblfriend.ClientID,lblmyname.ClientID,lblmyname1.ClientID);

Or

String.Format(Page.ClientScript.RegisterClientScriptBlock(this.GetType(), " ", "{3}('{0}').{4} ='{5}'; {3}('{1}').{4}='{6}';{3}('{2}').{4}='{7}'; return true;", true),lblfriend.ClientID,lblmyname.ClientID,lblmyname1.ClientID,"document.getElementById","innerHTML",text1,text2,text3);

#1


1  

You could expose the label text as string properties in the codebehind of the UserControl. Then when you render the usercontrol, pass the values to it and it would render the text of the labels.

您可以在UserControl的代码隐藏中将标签文本公开为字符串属性。然后,当您渲染usercontrol时,将值传递给它,它将呈现标签的文本。

Codebehind for the UserControl:

UserControl的Codebehind:

public partial class WebUserControl1 : System.Web.UI.UserControl
{
    private string _Friend;

    public string Friend
    {
        get
        {
            return _Friend;
        }
        set
        {
            _Friend = value;
            lblfriend.Text = value;
        }
    }

    private string _MyName;

    public string MyName
    {
        get
        {
            return _MyName;
        }
        set
        {
            _MyName = value;
            lblmyname.Text = value;
            lblmyname1.Text = value;
        }
    }

}

Then you could load your values as such in your parent control/page:

然后,您可以在父控件/页面中加载您的值:

        string text1 = fr.data[0].name;
        string text2 = m.data[0].name;                    
        MyUserControl.Friend = text1;
        MyUserControl.MyName = text2;

#2


4  

Look at the rendered page, are you sure your controls are named lblFriend and so on, I'm assuming they have a little more to it than that.

看看渲染的页面,你确定你的控件被命名为lblFriend等等,我假设它们还有更多的东西。

You may need to use

您可能需要使用

"document.getElementById('" + lblfriend.ClientID + "').innerHTML ='

#3


0  

Write only one RegisterClientScript with control's ClientID e.g.

只写一个RegisterClientScript和控件的ClientID,例如

String.Format(Page.ClientScript.RegisterClientScriptBlock(this.GetType(), " ", "document.getElementById('{0}').innerHTML ='" + text1 + "'; document.getElementById('{1}').innerHTML ='" + text2 + "';document.getElementById('{2}').innerHTML ='" + text3 + "'; return true;", true),lblfriend.ClientID,lblmyname.ClientID,lblmyname1.ClientID);

Or

String.Format(Page.ClientScript.RegisterClientScriptBlock(this.GetType(), " ", "{3}('{0}').{4} ='{5}'; {3}('{1}').{4}='{6}';{3}('{2}').{4}='{7}'; return true;", true),lblfriend.ClientID,lblmyname.ClientID,lblmyname1.ClientID,"document.getElementById","innerHTML",text1,text2,text3);