通过CommandArgument传递两个值和其分裂 [英] Passing two values through CommandArgument and splitting it

查看:193
本文介绍了通过CommandArgument传递两个值和其分裂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的LinkBut​​ton(这是一个包裹里面直放站)的commandArgument属性-as 1与字符串两个值传递给第二个转发器,然后我尝试将它们分成两个值,这样我就可以把它们作为在我的ADO.NET代码的参数(参数的SqlCommand)....测试我的查询不返回任何结果,但如果我通过固定值的参数或更改参数的来源后(只是从一个文本框或查询字符串测试或某事),我得到我的结果,所以我觉得这个问题是在分裂。



我从首位,而其包裹内LinkBut​​ton的的ArgumentCommand财产进行一些arugment值中继器:

 <&ItemTemplate中GT; 
< ASP:LinkBut​​ton的ID =sort_lnkbtn文本='<%#的eval(值)%>'
CommandArgument ='<%#的String.Format({0} | {1}的eval(arrange_by_id)的eval(值))%GT;'=服务器>
< / ASP:LinkBut​​ton的>
< / ItemTemplate中>



然后我收到这些值,并将其切成两条信息:

 字符串sortByAndArrangeBy =(e.CommandArgument)的ToString(); 
的char [] =分隔{'|'};
的String [] = sortByAndArrangeByArray sortByAndArrangeBy.Split(分隔符);

现在的ado.net代码使用这个值作为

 使用(SqlConnection的CN1 =新的SqlConnection(ConfigurationManager.ConnectionStrings [testConnectionString]。的ConnectionString))
{
使用(CM1的SqlCommand =新的SqlCommand (SELECT [名] FROM品牌WHERE(名称,如@SearchString +'%'),CN1))
{
cm1.Parameters.Add(@ SearchString在System.Data.SqlDbType。字符);
cm1.Parameters [@ SearchString在]值= sortByAndArrangeByArray [1]。

cn1.Open();使用(SqlDataReader的DR1 = cm1.ExecuteReader())
{
List_rpt.DataSource = DR1
;
List_rpt.DataBind();
}
}
}


解决方案

下面是一个简单的解决方案:



包装你的项目模板控件中的中继器。该控件将具有相同的标记为您的项目模板没有绑定:



控制标记:

 < DIV> 
< ASP:LinkBut​​ton的ID =LnkBtnSort=服务器文本=排序的OnClick =LnkBtnSort_Clicked/>
< / DIV>



控制代码:

 公共类SomeControl 
{
公共事件的EventHandler点击;

公共字符串ArrangeById
{
集合{的ViewState [byid] =值; }
{返回的ViewState [byid]的ToString()。 }
}

公共字符串值
{
集合{的ViewState [VAL] =值; }
{返回的ViewState [VAL]的ToString(); }
}

保护无效LnkBtnSort_Clicked(对象发件人,EventArgs五)
{
如果(点击!= NULL)
{
本。点击(这一点,EventArgs.Empty);
}
}
}



所以现在处于转发所有你所要做的就是绑定的控件到的Container.DataItem的一个实例:

 <&ItemTemplate中GT; 
<按Ctrl:SomeControl
ID =someControl
=服务器
的OnClick =SomeControl_Clicked
ArrangeById ='<%#的eval( arrange_by_id)%>'
值='<%#的eval(值)%GT;/>
< / ItemTemplate中>



页/控制具有中继器将有一个简单的方法:

 保护无效SomeControl_Clicked(对象发件人,EventArgs五)
{
//这里投下发送给您的控制类型:

SomeControl CTRL =(SomeControl)发送;

串byId = ctrl.ArrangeById;
串VAL = ctrl.Value;
}

请注意:此代码可能不是100%正确的,但它说明了这一点。流程很简单 - 控件绑定的公共属性,无论你需要绑定。当点击该链接(你的控制之内),控制不传播此事件的页面。相反,它触发它自己的事件(点击),从而将信号发送到一个事件发生的页面。但是通过这样做,它改变了事件的给自己的源,而不是实际的链接按钮。页面处理该事件,大家都高兴。



这样,您就不必关心CommandArgument是什么...如果出现空,这意味着无论您的数据源是空的...还是其他什么东西在代码中发生的事情。


I'm using the commandArgument property of the LinkButton ( Which is wrapped inside a repeater ) to pass two values -as one string- to a second repeater then I try to split them into two values, So I could use them as parameters in my ADO.NET Code (SqlCommand Parameters)....after testing my queries don't return any results but If I passed fixed values for the parameter or change the source of the parameter (just for test from a textbox or querystring or something) I get my results, so I think the problem is in splitting.

I Conduct some arugment values from the ArgumentCommand property of the LinkButton -which is wrapped inside a repeater:

   <ItemTemplate>
         <asp:LinkButton id="sort_lnkbtn" Text='<%# Eval("value")%>' 
         CommandArgument='<%#string.Format("{0}|{1}",Eval("arrange_by_id"),Eval("value"))%>' runat="server">
      </asp:LinkButton>
   </ItemTemplate>

Then I receive these values and cut them into two pieces of information:

        string sortByAndArrangeBy = (e.CommandArgument).ToString();
        char[] separator = { '|' };
        string[] sortByAndArrangeByArray = sortByAndArrangeBy.Split(separator);

Now the ado.net code uses this values as a

using (SqlConnection cn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
                    {
                        using (SqlCommand cm1 = new SqlCommand("SELECT [name] FROM brands WHERE (name like @SearchString + '%' )", cn1))
                        {
                            cm1.Parameters.Add("@SearchString", System.Data.SqlDbType.Char);
                            cm1.Parameters["@SearchString"].Value = sortByAndArrangeByArray[1];

                            cn1.Open();
                            using (SqlDataReader dr1 = cm1.ExecuteReader())
                            {
                                List_rpt.DataSource = dr1;
                                List_rpt.DataBind();
                            }
                        }
                    }

解决方案

Here is a simple solution:

Wrap your item template for the repeater in a control. The control will have the same markup as your item template without the bindings:

Control Markup:

<div>
    <asp:LinkButton ID="LnkBtnSort" runat="server" Text="Sort" OnClick="LnkBtnSort_Clicked"/>
</div>

Control Code:

public class SomeControl
{
     public event EventHandler Click;

     public string ArrangeById 
     { 
         set { ViewState["byid"] = value; } 
         get { return ViewState["byid"].ToString(); } 
     }

     public string Value
     {
         set { ViewState["val"] = value; } 
         get { return ViewState["val"].ToString(); }
     }

     protected void LnkBtnSort_Clicked(object sender, EventArgs e)
     {
         if( Click != null )
         {
             this.Click(this, EventArgs.Empty);
         }
     }
}

So now in the repeater all you have to do is bind an instance of that control to the Container.DataItem:

<ItemTemplate>
    <ctrl:SomeControl 
         ID="someControl" 
         runat="server" 
         OnClick="SomeControl_Clicked"
         ArrangeById='<%# Eval("arrange_by_id") %>'
         Value='<%# Eval("value") %>' /> 
</ItemTemplate>

The page/control that has the repeater will have one simple method:

protected void SomeControl_Clicked(object sender, EventArgs e)
{
    //Here cast the sender to the type of control you made:

    SomeControl ctrl = (SomeControl)sender;

    string byId = ctrl.ArrangeById;
    string val = ctrl.Value;
}

Note: this code may not be 100% correct but it illustrates the point. The flow is simple - the control binds its public properties to whatever you need to bind. When the link is clicked (inside your control) the control doesn't propagate this event to the page. Instead it fires its own event (Click) thus sending a signal to the page that an event occured. however by doing so, it changes the source of the event to itself instead of the actual link button. The page handles the event and everyone is happy.

This way you don't have to care what the CommandArgument is... If this comes up empty, it means that either your data source is empty... or something else happened in the code.

这篇关于通过CommandArgument传递两个值和其分裂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆