首页 > 解决方案 > 在动态表中添加点击事件

问题描述

这是我的html文件代码:

<body >
<form id="form1" runat="server">
    <div id="menu">
         <asp:Label ID="Label2"  runat="server" Text="שלום" 
CssClass="greeting"></asp:Label>
         <asp:Label ID="Label3"  runat="server" Text="אורח" 
CssClass="greeting"></asp:Label>
        <nav id="mainNav">
            <ul>

                <asp:Image ID="Image2" runat="server" 
ImageUrl="~/Images/logo.jpg" CssClass="logo" />

                <li><a href="About.aspx">אודות</a></li>
                <li><a href="Contact.aspx">צור קשר</a></li>
                <li><a href="Registration.aspx">הרשמה</a></li>
                <li><a href="Login.aspx">התחברות</a></li>
                <li><a href="Menu.aspx">דף הבית</a></li>


            </ul>
        </nav>
    </div>

    <div class="loginFormBackground" dir="ltr"  style="position: absolute">
        <h1 style="color:deeppink; text-align:center"> קל להכנה</h1>
         <table style="width: 100%; text-align: center;" dir="rtl" 
class="loginDiv" align="center">
             <tr>  
            <td align="center">  
                <asp:PlaceHolder ID="DBDataPlaceHolder" runat="server"> 
</asp:PlaceHolder>  
            </td>  
        </tr>  
    </table>  
    </div>
</form>
</body>

这是我的服务器端代码:

public partial class Easy : System.Web.UI.Page
{
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    StringBuilder htmlTable = new StringBuilder();
    UserDetails curUser1;
    int count;
    string name = "";

protected void Page_Load(object sender, EventArgs e)
    {
        Session["recipeID"] = null;
        count = (int)(Session["count"]);
        curUser1 = (UserDetails)(Session["curUser"]);
        if (curUser1 != null)
        {
            Label3.Text = curUser1.firstName + " " + curUser1.lastName;
        }
        if (!Page.IsPostBack)
            BindData();
    }

    private void BindData()
    {
        string constr; //here is the connection string to database

        SqlConnection con = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand("SELECT * FROM tblRecipes", con);
        da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

        htmlTable.Append("<table border='1'>");
        //htmlTable.Append("<tr style='background-color:green; color: White;'><th> ID.</th><th>Name</th><th>Address</th><th>Contact No</th></tr>");

        if (!object.Equals(ds.Tables[0], null))
        {
            if (ds.Tables[0].Rows.Count > 0)
            {

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string idR = ds.Tables[0].Rows[i]["recipeID"].ToString();
                    string link = "";
                    if (!(DBNull.Value.Equals(ds.Tables[0].Rows[i]["recipeURL"].ToString())))
                        link = ResolveUrl(ds.Tables[0].Rows[i]["recipeURL"].ToString());
                    name = ds.Tables[0].Rows[i]["recipeName"].ToString();
                    string s = ResolveUrl((ds.Tables[0].Rows[i]["pic1"]).ToString());
                    string time = "";
                    if (!(DBNull.Value.Equals(ds.Tables[0].Rows[i]["preparingTime"].ToString())))
                        time = ds.Tables[0].Rows[i]["preparingTime"].ToString();
                    string kcl = "";
                    if (!(DBNull.Value.Equals(ds.Tables[0].Rows[i]["kalories"].ToString())))
                        kcl = ds.Tables[0].Rows[i]["kalories"].ToString();
                    string category = "";
                    if (!(DBNull.Value.Equals(ds.Tables[0].Rows[i]["category"].ToString())))
                        category = ds.Tables[0].Rows[i]["category"].ToString();
                    string dificult = "-----";
                    if (!(DBNull.Value.Equals(ds.Tables[0].Rows[i]["skillLevel"].ToString())))
                        dificult = ds.Tables[0].Rows[i]["skillLevel"].ToString();
                    if ((s.Contains("easy") || category.Contains("קל")) && ds.Tables[0].Rows[i]["show"].ToString().Equals("yes"))
                    {
                        htmlTable.Append("<tr style='color: deeppink; font-weight: bold; text-align: center;'>");
                        //htmlTable.Append("<td clickOn='"+"Function()"+"'>");
                        htmlTable.Append("<td>");
                        //htmlTable.Append(name);

                        if (link.Equals(""))
                        {
                            //htmlTable.Append( ds.Tables[0].Rows[i]["recipeAuthor"]);
                            //htmlTable.Append("<a href='" + "ShowRecipe.aspx" + "'>" + name + " </a>");              
                            //Session["recipeID"] = null;
                            //htmlTable.Append("<a href= '" + Function(name)+"'>" + name + "</a>");
                            //Response.Write("<script>alert('yes')</script");
                            //htmlTable.Append("<a href=ShowRecipe.aspx value='"+idR+"'>" + name + "</a>");

                            Button btn = new Button();
                            btn.Text = name;
                            btn.Click += new EventHandler(btn_Click);
                            htmlTable.Append("<input type='submit' name='name' value='name' runat='server' onclientclick='tn_Click'/>");

                            //htmlTable.Append("<Button text='" + name + "'onClick='"+ btn_Click() +"'/>");


                        }
                        else
                        {
                            Response.Write("<script>alert('no')</script");
                            htmlTable.Append("<a href='" + link + "'>" + name + "</a>");
                        }
                        htmlTable.Append("</br>" + "רמת קושי:  " + dificult);
                        htmlTable.Append("</br>" + "קלוריות:" + kcl);
                        if (!time.Equals(""))
                            htmlTable.Append("</br>" + "זמן הכנה:" + time + "  דקות ");
                        else
                            htmlTable.Append("</br>" + "זמן הכנה:" + time);
                        htmlTable.Append("</td>");
                        htmlTable.Append("<td>" + " מאת  " + ds.Tables[0].Rows[i]["recipeAuthor"] + "</td>");
                        htmlTable.Append("<td>");
                        htmlTable.Append("<img src='" + s + "'visible='true' runat='server' style='width:300px; height:200px;'/>");
                        htmlTable.Append("</td>");
                        htmlTable.Append("</tr>");
                    }
                }
                htmlTable.Append("</table>");
                DBDataPlaceHolder.Controls.Add(new Literal { Text = htmlTable.ToString() });
            }
            else
            {
                htmlTable.Append("<tr>");
                htmlTable.Append("<td align='center' colspan='4'>There is no Record.</td>");
                htmlTable.Append("</tr>");
            }
        }



    }

    private void btn_Click(object sender, EventArgs e)
    {


    }

我试图在所有没有链接的食谱中创建动态按钮(指向其他网站的网址)。在这种情况下,我必须使用特定配方创建指向其他 Web 表单的链接以显示其详细信息。我对服务器端内的 html 代码语法有点迷失了。当我尝试 onclick 事件时,它不起作用。我可能确定我没有以正确的方式这样做。请帮忙。

标签: c#htmlasp.netweb

解决方案


推荐阅读