2009-08-03 28 views
0

私は不思議です、どうやってサーバ側の関数にパラメータを渡すのですか?だから、合格する問題もあるパラメータを持つサーバサイド関数

<asp:LinkButton ID="lkbChange" runat="server" OnClick="lkbChange_Click(<%= value %>)" /> 

protected void lkbChange_Click(Object sender, EventArgs e, string value) 
{ 
    Session["location"] = value; 
} 

<script runat="server"> 
protected void lkbChange_Click(Object sender, EventArgs e) 
{ 
    Session["location"] = new_value; 
} 
<script > 

そして、私のような何かをしたいと思います:

<asp:LinkButton ID="lkbChange" runat="server" OnClick="lkbChange_Click" />

はその後、サーバー側の機能は、次のようになりますパラメータを関数呼び出しに...

EDIT

[OK]を、私はrunat = "サーバー"が間違っていると感じ始めています。まあ、私はいくつかのマシンとそこにプロパティを提示しています。私がここで欲しいのは、これらのマシンの場所に基づいて(マシンの内容、履歴などの)現在の結果をフィルタリングすることです。これは、現在のアクションが同じままでなければならないことを意味します。現在選択されている場所にフィルタリングされた、リンクから取得された、セッション["location"]の値をどこにでも。

+0

asp.net-MVCのために本当にこのですか?あなたはそれを間違ってタグ付けしてしまったのか、まったく間違っているのか... – Charlino

+0

はい、それはMVCですが、私はこのプロセスがindefferentなので、アクションでは処理したくありません。 – Trimack

+0

これはMVC用の場合、runat = "server"を使用するのは完全に間違っていて、実際には何もしてはいけません。おそらくあなたがやろうとしていることに少しだけ詳しい情報を与えることができますか?例えば。価値は何を表していますか、アプリケーションのコンテキストでボタンは何をしていますか? – Charlino

答えて

1

[OK]を、私はまだあなたが実際に必要なものに少しぼんやりですが、私はそれを与えるでしょう(私はそれが答えよりも多くの質問を発生させる恐れがありますが;-))。あなたのglobal.asax.csで

これに似たルートを追加:

<%= Html.ActionLink("link text", 
        "AddLocationFilter", 
        new { filterValue = value })%> 
:次に、あなたのビュー内のすべてのリンクは(「の値が」あなたのフィルタ値です)のようになる

routes.MapRoute(
    "MachineList", 
    "Machines/AddLocationFilter/{filterValue}", 
    new { controller = "Machines", action = "AddLocationFilter", filterValue = "" } 
); 

public ActionResult Index() 
{ 
    //Get your machines here 
    //NOTE: I have no idea how you want this to work and this is just an example of how it could work 
    IQueryable<Machine> machines = myMachineRepository.GetAllMachines(); 

    //Use the "FilterLocations" session value if it exists 
    if (Session["FilterLocations"] != null) 
    { 
     IList<string> filterLocations = Session["FilterLocations"] as List<string> 
     machines.Where(m => Locations.Contains(m.Location)) 
    } 

    //Now send the filtered list of machines to your view 
    return View(machines.ToList()); 
} 

public ActionResult AddLocationFilter(string filterValue) 
{ 
    //If there isn't a filterValue don't do anything 
    if (string.IsNullOrEmpty(filterValue)) 
     return RedirectToAction("index"); 

    IList<string> filterLocations; 
    //Get it from session 
    if (Session["FilterLocations"] != null) 
     filterLocations = Session["FilterLocations"] as List<string> 

    //If it's still null create a new one 
    if (filterLocations == null) 
     filterLocations = new List<string>(); 

    //If it doesn't already contain the value then add it 
    if (!filterLocations.Contains(filterValue)) 
     filterLocations.Add(filterValue); 

    //Finally save it to the session 
    Session["FilterLocations"] = filterLocations; 

    //Now redirect 
    return RedirectToAction("index"); 
} 

精通:あなたのマシンコントローラで今

最後にリダイレクトに満足しているわけではありませんが、わかりやすく簡単に理解できるようにしました。あなたは、jQueryでいくつかの非常に流線型でセクシーなことをすることができ、JSONの結果を返すことができます...しかし、それは別のレベルです。

HTHS
チャールズ

+0

私は本当にそれをappriciate、しかしこれはまさにそれではありません。これがうまくいく(リダイレクトする)アクションがいくつかあります。修正インデックスの代わりに最後に使用された(現在のユーザーのために)アクションにリダイレクトすることが可能な場合は、それが完璧になるでしょう。 – Trimack

+0

あなたは次のようなことを意味します:Redirect(Request.UrlReferrer.ToString()); ? – Charlino

+0

それは:)私は.NETに新しいので、私はすべての時間を失うので:)再びありがとう。 – Trimack

1

値とはそれはLinkBut​​tonのいくつかのプロパティですか?はいの場合は、送信者からアクセスできます。

それ以外の場合は、AFAIKが前述の方法を使用するか、カスタムイベントハンドラを書き込むことができます。

+0

値は、linkbuttonがセッション変数に保存する必要がある値ではなく、リンクボタンのプロパティではありませんが、プロパティの良い考えに感謝します。私は間違いを犯したが、うまくいかなかった。おそらくなぜ今知っている。 – Trimack

1

これを達成するには、CommandArgumentとCommandNameを使用できます。

<asp:LinkButton ID="lkbChange" runat="server" CommandArgument="MyArg" 
CommandName="ThisLnkClick" OnClick="MyHandler" /> 

あなたはハンドラを作成します。

protected void MyHandler(Object sender, EventArgs e) 
{ 

     Button lnkButton = (Button)sender; 
     //You could handle different command names by doing something like: if (lnkButton.CommandName == "ThisLnkClick") ... 
     CustomMethod(lnkButton.CommandArgument.ToString()); 
} 

あなたは、パラメータを取り、あなたのカスタムメソッドを作成する必要があります。

関連する問題