如何获取运行时控件id

本文关键字:运行时 控件 id 获取 何获取 | 更新日期: 2023-09-27 18:06:42

使用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不存在。

需要代码帮助

如何获取运行时控件id

您的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方法更加可重用,因为它不再与UI耦合。

删除方法中的static

将此代码放在public void Insert()而不是静态void Insert((