2

テーブルを動的に作成しています。各行にはボタンが含まれ、このボタンは関数を呼び出します。たとえば、各ボタンはさまざまなパラメーターを処理します。

    string Parameter1 = "MyValue";
    string Parameter2 = "MyOthervalue";

    HtmlInputButton input = new HtmlInputButton();
    input.ID = "Button1";
    input.Value = "button";
    input.Attributes.Add("onclick", "MyFunction(" + Parameter1 + "," + Parameter2 + ");");

    td = new HtmlTableCell();
    td.Align = "center";
    td.Controls.Add(input);
    tr.Cells.Add(td);

ユーザーがボタンをクリックしたら、MyFunctionを実行してから、ページを更新して変更を確認する必要があります。

2番目のページを開いて、URLでパラメーターを送信できる可能性があることはわかっていますが、同じページでコードを使用してから、ページをリロードしたいと思います。

最善のアプローチは何ですか。

ありがとう、

編集。

私はそれを次のように解決することができました:

    input = new Button();
    input.Text = "Click Me";
    input.CommandArgument = "3|Parameter3";
    input.Command += new CommandEventHandler(Button_Handler);

    td = new HtmlTableCell();
    td.Align = "center";
    td.Controls.Add(input);

    tr.Cells.Add(td);
    tbTable.Rows.Add(tr);



public void Button_Handler(object sender, CommandEventArgs e)
{
    Button mybutton = (Button)sender;
    string[] Parameters = e.CommandArgument.ToString().Split('|');
    mybutton.Text = Parameters[0] + "-" + Parameters[1];
    //Call MyFunction(Parameters[0], Parameters[1]);

}
4

3 に答える 3

1

MyFunction が JavaScript 関数の場合location.reload()、ページをリロードするために最後に追加できます。

于 2013-01-11T07:45:05.400 に答える
1

ここでは、クリックイベントを動的に追加する方法についていくつかのサンプル コードを作成しました。

  void Page_Load(Object sender, EventArgs e)
  {
    HtmlInputButton NewButtonControl = new HtmlInputButton("submit");
    NewButtonControl.ID = "NewButtonControl";
    NewButtonControl.Value = "Click Me";

    // Create an EventHandler delegate for the method you want to handle the event
    // and then add it to the list of methods called when the event is raised.
    NewButtonControl.ServerClick += new System.EventHandler(this.Button_Click);

    // Add the new HtmlInputButton control to the Controls collection of the
    // PlaceHolder control. 
    ControlContainer.Controls.Add(NewButtonControl);
  }

  void Button_Click(Object sender, EventArgs e)
  {
    // Display a simple message. 
    Message.InnerHtml = "Thank you for clicking the button.";
  }

ページを更新するには、次のような Javascript 関数を呼び出すことができます。

<script type="text/javascript">
  function reloadPage()
  {
    window.location.reload()
  }
</script>

またはこれを試してください:

Response.Redirect(Request.Url.AbsoluteUri);

役立つことを願っています

于 2013-01-11T07:55:16.263 に答える
1

非表示のボタンと非表示のフィールドを使用できるトリックがあります。

HTML:

<script type="text/javascript">
    function MyFunction() {
        // do something
        document.getElementById("<%= hid.ClientID %>").value = "some value";

        // trigger hidden button click
        document.getElementById("<%= hiddenButton.ClientID %>").click();
    }
</script>

<asp:HiddenField ID="hid" runat="server" />
<asp:Button ID="hiddenButton" runat="server" OnClick="hiddenButton_Click" style="display:none" />

コードビハインド:

protected void hiddenButton_Click(object sender, EventArgs e)
{
     // your business goes here
     string value = hid.Value;
}
于 2013-01-11T07:42:41.550 に答える