如何使用html按钮触发c#函数

时间:2014-05-30 15:49:22

标签: c# asp.net

我使用asp:按钮将文本框中的数据绑定到网格中,但我的问题是让页面停止刷新。所以我将asp:按钮更改为html按钮,因为它是服务器端导致页面刷新。现在我不知道如何使用相同的c#代码使用html按钮绑定数据。

以下是我的c#代码示例:

    //Site Class
    class Sites
    {
    //Strings and variables are created based off text box Id to make matter easier to read
        public string SiteName { get; set; }
        public string Slick { get; set; }
        public string UserID { get; set; }
    }

    //Created a new list based on class.
    //Items are added based on whats in the textboxes 
    //and events are fired to gridview by the click of the class button
    protected void SiteDetails_Click(object sender, EventArgs e)
    {
        List<Sites> list;

        if (Session["list"] == null)
        {
            list = new List<Sites>();
        }
        else
        {
            list = (List<Sites>)Session["list"];
        }
        list.Add(new Sites() { SiteName = Sname.Text, Slick = Slick.Text, UserID = User_ID.Text });
        Session["list"] = list;
        SiteGrid.DataSource = list;
        SiteGrid.DataBind();
    }

这是我的Html aspx页面:

网站详情

                                                                  
                                                                                                                                    网站名:                                                              

                        <td style="width: 102px; height: 8px;">Slic:</td>
                        <td style="width: 153px; height: 8px;">
                            <asp:TextBox ID="Slick" runat="server"></asp:TextBox>
                        </td>
                    </tr>

                    <tr>
                        <td style="width: 129px; margin-left: 40px; height: 23px;">User ID:</td>
                        <td style="width: 152px; height: 23px;">
                            <asp:TextBox ID="User_ID" runat="server" style="font-weight: bold"></asp:TextBox>
                            </td>

                        <td style="width: 102px; height: 23px;">
                            Password:</td>
                        <td style="width: 153px; height: 23px;">
                            <asp:TextBox ID="Pass" runat="server" TextMode="Password" ></asp:TextBox>
                        </td>

                    </tr>
                   <%-- //Site Button--%>
                    <tr>
                                                                  <td></td>
                        <td>
                  <button ID="SiteDetails" OnClick="SiteDetails_Click()">AddSites</button>
                        </td>
                    </tr>
                    </table>

                  <%-- //Site GridView--%>
       <asp:GridView ID="SiteGrid" class="table-bordered table-hover tableGridLines="None">
                      </asp:GridView>

0 个答案:

没有答案