首页 > 解决方案 > open form on html button click in asp.net using C#

问题描述

will any body help me in opening a form on button click?? Thanks in advance.

aspx.cs Code:

        public string wogrid()
        {
            string htmlStr = "";
            con.COpen();
            string qry = "SELECT id,casetype,Case when status_c=1 then 'Active' else 'Inactive' End as status_c FROM t_claimtype";
            SqlDataReader rd = gd.DataReader(qry);
            while (rd.Read())
            {
                int ID = Convert.ToInt16(rd["id"].ToString());
                string caseType = rd["casetype"].ToString();
                string status = rd["status_c"].ToString(); 
                htmlStr += "<tr><td>" + ID + "</td>" + "<td>" + caseType + "</td>" + "<td>" + status + "</td>" + "<td>" + "<input type='submit' id='" + (rd)["ID"].ToString() + "' name='edit' value='EDIT' **onclick='windows.open(viewClaims.aspx)'** runat='server' />" + "</td></tr>";

            }
            con.cClose();
            return htmlStr;
        }
private void AddPlanToCart()
        {
            Response.Redirect("viewClaims.aspx");
        }

标签: c#asp.net

解决方案


This results in invalid JavaScript:

onclick='windows.open(viewClaims.aspx)'

Because:

  • There is no windows object, it's called window.
  • You didn't enclose the string "viewClaims.aspx" in quotes.

Additionally, HTML attributes should be enclosed in double-quotes. JavaScript strings can be single-quoted or double-quoted, so you have flexibility there. In order to do this on your current line of code you will need to "escape" some of the quotes. (This should be expected any time you try to mix three different languages on one line of code.)

此外,<input/>不应包含此元素,runat="server"因为它不是服务器端控件。该属性对网络浏览器没有任何意义,只会被忽略。

(同时删除这些*字符。我确定它们在那里是为了在您的帖子中突出显示该部分代码,但实际上它们是无效代码,应该被删除。)

将这些放在一起,您当前拥有的这个字符串文字:

"' name='edit' value='EDIT' **onclick='windows.open(viewClaims.aspx)'** runat='server' />"

会变成这样:

"' name=\"edit\" value=\"EDIT\" onclick=\"windows.open('viewClaims.aspx')\" />"

从语义上讲,你真的不应该首先为此使用按钮。您正在构建的功能是在用户单击某些内容时将其引导到另一个页面。一个链接已经做到了。默认情况下,它不需要 JavaScript 或试图绕过使用表单或任何其他黑客攻击。

只需使用链接:

"<a href=\"viewClaims.aspx\" id=\"" + (rd)["ID"].ToString() + "\">Edit</a>"

推荐阅读