如何获取运行时控件ID

时间:2013-04-01 08:40:51

标签: c# asp.net

使用asp.net和c#

Asp.net代码

<asp:Button ID="btnAddNew" runat="server" Text="Add New" 
        onclick="btnAddNew_Click" />
    <div id="divAdd" runat="server" style="display:none" >  
    <table width="100%">
        <tr>
            <td colspan="1">First Name :</td>
            <td colspan="1" align="left"><asp:TextBox ID="txtFName" runat="server" ></asp:TextBox> </td>
        </tr>
        <tr>
            <td colspan="1">Last Name :</td>
            <td colspan="1" align="left"><asp:TextBox ID="txtLName" runat="server" ></asp:TextBox>  </td>
        </tr>
        <tr>
            <td colspan="1">Date Of Birth :</td>
            <td colspan="1" align="left"><asp:TextBox ID="txtDob" runat="server" ></asp:TextBox>  </td>
        </tr>
        <tr>
            <td colspan="1">Age :</td>
            <td colspan="1" align="left" ><asp:TextBox ID="txtAge" runat="server" ></asp:TextBox> </td>
        </tr>
        <tr>

            <td colspan="2" align="center">
                <asp:Button ID="btnSave" runat="server" Text="Save"  OnClientClick=" return validate()" type="submit"   />

                <asp:Button ID="btnClose" runat="server" Text="Close" OnClientClick ="return clear()" type="submit" />
            </td>
        </tr>
    </table>
    </div>

C#代码

static void Insert()
    {
        try
        {
            string connectionString =
                "server=JEBW1011ZAHID;" +        
                "initial catalog=employee;" + 
                "integrated security = true";             
            using (SqlConnection conn =
                new SqlConnection(connectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(
                    "INSERT INTO [NyhedTB] ([NyhedDato], [NyhedTitel], [NyhedTekst]) " +
                    "VALUES (@NyhedDato, @NyhedTitel, @NyhedTekst)", conn))
                {
                    cmd.Parameters.AddWithValue("@NyhedDato", txtfname.Text);
                    cmd.Parameters.AddWithValue("@NyhedTitel", txtlanme.Text);
                    cmd.Parameters.AddWithValue("@NyhedTekst", txtage.Text);

                    int rows = cmd.ExecuteNonQuery();  // Inserted rows number
                }
            }
        }
        catch (SqlException ex)
        {
            //Log exception
            //Display Error message
        }

    }

显示错误,因为txtfname不存在。

需要代码帮助

2 个答案:

答案 0 :(得分:6)

Insert方法是静态的。它无权访问任何页面元素。我建议您将这些值作为参数传递给方法,而不是将其与您的UI耦合:

static void Insert(string fName, string lName, string age)
{
    try
    {
        string connectionString =
            "server=JEBW1011ZAHID;" +        
            "initial catalog=employee;" + 
            "integrated security = true";             
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(
                "INSERT INTO [NyhedTB] ([NyhedDato], [NyhedTitel], [NyhedTekst]) " +
                "VALUES (@NyhedDato, @NyhedTitel, @NyhedTekst)", conn))
            {
                cmd.Parameters.AddWithValue("@NyhedDato", fName);
                cmd.Parameters.AddWithValue("@NyhedTitel", lName);
                cmd.Parameters.AddWithValue("@NyhedTekst", age);

                int rows = cmd.ExecuteNonQuery();  // Inserted rows number
            }
        }
    }
    catch (SqlException ex)
    {
        //Log exception
        //Display Error message
    }
}

然后从后面的代码调用此方法时,您可以传递任何您想要的值:

protected void SomeButton_Click(object sender, EventArgs e)
{
    Insert(txtfname.Text, txtlanme.Text, txtage.Text);
}

现在,您的Insert方法可以重复使用,因为它不再与您的用户界面相关联。

答案 1 :(得分:0)

删除方法中的static

使用此代码public void Insert()代替static void Insert()