简体   繁体   English

将文本存储为变量,然后在ASP.net中调用

[英]Store text as variable then call in ASP.net

I'm working on making customized gridviews with the use of dropdownlist and checkboxes. 我正在使用dropdownlist和复选框来制作自定义的gridview。 The checkboxes represent the columns in the database to call. 复选框代表数据库中要调用的列。 My codebehind file is to create a customized SQL Query that I call back to the asp:DqlDataSource... SelectCommand="" 我的代码隐藏文件是创建一个自定义的SQL查询,我将该查询返回给asp:DqlDataSource ... SelectCommand =“”

As of now I'm trying to store a message into (query) into a variable, this is where I'm currently stuck... 截至目前,我正在尝试将消息存储到(查询)到变量中,这是我目前遇到的问题...

<asp:DropDownList ID="DDL" runat="server" DataSourceID="SqlDataSource1" 
        DataTextField="Fullname" DataValueField="Employee_ID"  AutoPostBack="true">
    </asp:DropDownList> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" 
        SelectCommand="@Query">
    </asp:SqlDataSource>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    <asp:CheckBox ID="Address" Text="Address" runat="server" />
    <asp:CheckBox ID="Phone" Text="Phone" runat="server" />
    <asp:CheckBox ID="Email" Text="Email" runat="server" />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="ButtonID"  onclick="Create" runat="server" Text="Submit" />
    <br />
    <br />
    <br />
    <br />
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label> -
    <asp:Label ID="Label2" runat="server" Text=""></asp:Label> -
    <asp:Label ID="Label3" runat="server" Text=""></asp:Label> -
    <asp:Label ID="Label4" runat="server" Text=""></asp:Label> -
    <br />
    <asp:Label ID="Label5" runat="server" Text=""></asp:Label>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>

======================= CodeBehind =========================== =======================代码隐藏========================== =

protected void Create(object sender, System.EventArgs e)
{

    string a = "";
    string b = "";
    string c = "";
    string d = "";

    if (DDL.SelectedValue == "Select")
    {
        return;
    }
    else
    {
        a = DDL.SelectedValue;
    }
    if (Address.Checked == true)
    {
        b = "Address,";

    }

    if (Phone.Checked == true)
    {
        c = "Phone,";

    }

    if (Email.Checked == true)
    {
        d = "Email,";

    }

        Label1.Text = a;
        Label2.Text = b;
        Label3.Text = c;
        Label4.Text = d;
        string Query = ("SELECT" b c d "FROM Employee WHERE Employee_ID =" a);    
}

Is String concatenation what you are looking for? 您正在寻找String级联吗?

Try using this: 尝试使用此:

string Query = String.Format("SELECT {0} {1} {2} FROM Employee WHERE Employee_ID ={3}",b,c,d,a);

What exactly is the issue you're running in to? 您遇到的问题到底是什么?

I would guess that you're having an issue running that query because it'll have a comma before FROM . 我猜您在运行该查询时遇到了问题,因为它在FROM之前会有一个逗号。 A better way to hold the fields you want to search on might be to push the values into a single array and then loop through the contents of the array and separate each by a comma. 保留要搜索的字段的一种更好的方法可能是将值推入单个数组中,然后遍历数组的内容并用逗号分隔每个字段。 That way your list of fields can grow and shrink to any size without worrying about empty values. 这样,您的字段列表可以增长和缩小到任意大小,而不必担心空值。 You also might want to consider wrapping the "a" value in single quotes (assuming you're reading in strings...) 您可能还需要考虑将“ a”值包装在单引号中(假设您正在读取字符串...)

Old code: 旧代码:

string Query = ("SELECT" b c d "FROM Employee WHERE Employee_ID =" a); 

Result: *SELECT Address,Phone,Email,FROM Employee WHERE Employee_ID = Bob Jones* 结果: *选择地址,电话,电子邮件,从雇员那里雇员ID =鲍勃·琼斯*

New code: 新代码:

string Query = "SELECT " + String.Join(", ", arrFields) + " FROM Employee WHERE Employee_ID = '" + a + "'"

Result: *SELECT Address, Phone, Email FROM Employee WHERE Employee_ID = 'Bob Jones'* 结果: *选择来自员工的地址,电话,电子邮件WHERE Employee_ID ='Bob Jones'*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM