简体   繁体   English

ASP.NET可以将CommandArgument Eval收入2列吗?

[英]ASP.NET can CommandArgument Eval take in 2 columns?

I have a CommandArgument that takes in 1 argument from a LinkButton in my GridVie for row deletion in GridView. 我有一个CommandArgument,从GridView中的LinkBut​​ton接收1个参数,用于GridView中的行删除。

Code in my aspx.cs: 我的aspx.cs中的代码:

LinkButton lnk = (LinkButton)sender;
        string stid = lnk.CommandArgument.ToString();
        SqlConnection conn = new SqlConnection("<Connection>");
        string sql = string.Format("DELETE FROM [ConflictingRole] where Role like '%{0}%'",stid);
        SqlCommand cmd = new SqlCommand(sql, conn);
        conn.Open();
        cmd.ExecuteNonQuery();

Code in my .aspx: 我的.aspx中的代码:

<asp:GridView ID="GridView1" ondatabound="gv_DataBound" runat="server" DataSourceID="SqlDataSource1">
    <Columns>
        <asp:TemplateField>
           <ItemTemplate>

             <asp:CheckBox ID="UserSelection" OnCheckedChanged="CRUserSelector_CheckedChanged" ondatabound="gv_DataBound" runat="server" />
             <asp:LinkButton ID="lnkDelete" runat="server" onclick="lnkDelete_Click" Text="Delete" CommandArgument='<%# Eval("Role") %>' PostBackUrl="~/CRList.aspx" ></asp:LinkButton>

           </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

My current CommandArgument only takes in one column name, which is Role. 我当前的CommandArgument只接受一个列名,即Role。 However, I would like to take in 2 column names, and use the 2 column names in my String sql statement in aspx.cs. 但是,我想接受2个列名,并在aspx.cs中的String sql语句中使用2列名。

Reason: I have multiple rows with the same value of "Row", thus if I delete based on the "Role" column's value, I will delete multiple rows. 原因:我有多行具有相同的“行”值,因此如果我根据“角色”列的值删除,我将删除多行。 I would want to take in values from both "Role" and "ConflictingRole" for the delete sql query. 我想从删除sql查询中获取“Role”和“ConflictingRole”的值。

May I know if it is possible to do so? 我可以知道是否可以这样做?

Thank You. 谢谢。

You may pass value of two or more columns by converting them to string with comma delimiter. 您可以通过使用逗号分隔符将它们转换为字符串来传递两列或更多列的值。

CommandArgument='<%# Eval("Role") + "," + Eval("ConflictingRole") %>'

split the commandArgument value in the handler, 拆分处理程序中的commandArgument值,

string []ar=lnk.CommandArgument.ToString().Split(',');

EDIT: 编辑:

You may write delete query like: 您可以编写删除查询,如:

string sql="DELETE FROM [ConflictingRoles] where Role like @role AND ConflictingRole like @crole";

OR 要么

string sql="DELETE FROM [ConflictingRoles] where Role=@role AND ConflictingRole=@crole";

Or Hard coded sql statement (which is not recommend) 或硬编码的sql语句(不推荐)

string sql=string.Format("DELETE FROM [ConflictingRoles] where Role like '%{0}%' AND 
          ConflictingRole like '%{1}%'",ar[0],ar[1]);

Within your linkbutton you could change the command argument to this. 在您的linkbutton中,您可以将命令参数更改为此。

CommandArgument='<%# Eval("Role")+"::"+Eval("ConflictingRole") %>' 

Then within your codebehind you could access both by doing the below. 然后在您的代码隐藏中,您可以通过执行以下操作来访问它们。

string[] both = lnk.CommandArgument.ToString().Split(new string[]{"::"},StringSplitOptions.None);

string first = both[0];
string second = both[1];

Not very elegant ... but i guess it'll solve your need. 不是很优雅......但我猜它会解决你的需求。

Try the code below. 请尝试下面的代码。

string[] both = lnk.CommandArgument.ToString().Split(new string[]{"::"},StringSplitOptions.None);

string first = both[0];
string second = both[1];

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

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