c# - button1.Attributes.Add("onclick", "javascript:window.open('page1.aspx?CheckedItem=&q

my client side code button1.Attributes.Add("onclick", "javascript:window.open('page

my client side code button1.Attributes.Add("onclick", "javascript:window.open('page1.aspx?CheckedItem=" + checkedItem.ToString() + "','mywindow',');"); is not executing on the first click.But it is working fine from second click onwards.I wrote this code inside the button click event.

I had tried to put this inside the page_load ,but the problem is i need to return a value from this page. here is the code

protected void btnPreview_Click(object sender, EventArgs e)
{

    //StringBuilder checkedItem = new StringBuilder();
    checkedItem.Length = 0;
    foreach (ListItem i in chkValidation.Items)
    {
        if (i.Selected)
        {
            if (string.IsNullOrEmpty(Convert.ToString(checkedItem)))
            {
                checkedItem.AppendFormat(i.Text);

            }
            else
            {
                checkedItem.AppendFormat(",");
                checkedItem.AppendFormat(i.Text);
            }
        }
        btn_Preview.Attributes.Add("onclick",
                                   "javascript:window.open('TimePhaseAttributePreview.aspx?CheckedItem=" + 
                                   checkedItem.ToString() + "','mywindow','menubar=0,resizable=0,width=350,height=250');");
    }

Please help me

my client side code button1.Attributes.Add("onclick", "javascript:window.open('page1.aspx?CheckedItem=" + checkedItem.ToString() + "','mywindow',');"); is not executing on the first click.But it is working fine from second click onwards.I wrote this code inside the button click event.

I had tried to put this inside the page_load ,but the problem is i need to return a value from this page. here is the code

protected void btnPreview_Click(object sender, EventArgs e)
{

    //StringBuilder checkedItem = new StringBuilder();
    checkedItem.Length = 0;
    foreach (ListItem i in chkValidation.Items)
    {
        if (i.Selected)
        {
            if (string.IsNullOrEmpty(Convert.ToString(checkedItem)))
            {
                checkedItem.AppendFormat(i.Text);

            }
            else
            {
                checkedItem.AppendFormat(",");
                checkedItem.AppendFormat(i.Text);
            }
        }
        btn_Preview.Attributes.Add("onclick",
                                   "javascript:window.open('TimePhaseAttributePreview.aspx?CheckedItem=" + 
                                   checkedItem.ToString() + "','mywindow','menubar=0,resizable=0,width=350,height=250');");
    }

Please help me

Share Improve this question edited Jan 3, 2012 at 8:40 albertjan 7,8276 gold badges46 silver badges77 bronze badges asked Jan 3, 2012 at 8:06 user1117040user1117040 1412 gold badges2 silver badges12 bronze badges 5
  • Given your "I wrote this code inside the button click event" I will need to see the code! Got a jsfiddle? Also, don't put javascript: in the value of an onclick attribute. – Mr Lister Commented Jan 3, 2012 at 8:10
  • hai MrLister,here is the code – user1117040 Commented Jan 3, 2012 at 8:11
  • not related to the answer. above code you are adding attribute in a loop. it shoud be outside of the loop – Chamika Sandamal Commented Jan 3, 2012 at 9:26
  • @MrLister why not putting javascript there? – draks ... Commented Oct 26, 2012 at 7:35
  • Because it's redundant. onclick="dosomething()" has the same result as onclick="javascript:dosomething()" so you don't need the javascript:. The colon also makes it look (to the untrained eye) like a URL. – Mr Lister Commented Oct 26, 2012 at 9:16
Add a ment  | 

2 Answers 2

Reset to default 0

put the code in any page event. like page_load,init.. or use RegisterClientScriptBlock

You need to use OnClientClick, not OnCLick

Change your code to

button1.OnClientClick = "javascript:window.open('page1.aspx?CheckedItem=" + checkedItem.ToString() + "'
                        ,'mywindow',')";

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744898108a4599817.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信