是否可以在C#方法中使用JavaScript函数

时间:2012-08-13 11:10:17

标签: c# javascript asp.net listview dialog

我使用2个WebForms ans我想使用一个webform,我希望用第二个Webform打开一个Dialog。 我想打开Dialog如果我从ListView点击一个LinkBut​​ton它工作正常,但我需要第二个Webform的值,所以我首先打开对话框。此对话框为空。关闭此对话框后,值将发送到会话。

ASPX:

...
<script type="text/javascript">

         function OpenChild() {

             var WinSettings = "center:yes;resizable:no;dialogHeight:550px"
             var MyArgs = window.showModalDialog("Benutzer.aspx", MyArgs, WinSettings);

         }

    </script>
...
<asp:ListView runat="server" ID="myListView">

        <LayoutTemplate>
            <table id="UserTable" runat="server" border="0" width="800" cellpadding="0" cellspacing="0">
                <tr style="background-color:#E5E5FE" class="TableClass">
                    <th id="th1" runat="server"><asp:LinkButton ID="lnkBenutzer" runat="server" >id_Benutzer</asp:LinkButton></th>
                    <th id="th2" runat="server"><asp:LinkButton ID="lnkemail" runat="server" >id_Email</asp:LinkButton></th>
                    <th id="th3" runat="server"><asp:LinkButton ID="lnkVorname" runat="server" >id_Vorname</asp:LinkButton></th>
                    <th id="th4" runat="server"><asp:LinkButton ID="lnkNachname" runat="server" >id_Nachname</asp:LinkButton></th>
                    <th id="th5" runat="server"><asp:LinkButton ID="lnkTelefon" runat="server" >id_Telefon</asp:LinkButton></th>
                </tr>
                <tr runat="server" id="ItemPlaceholder">
                </tr>
            </table>
        </LayoutTemplate>

        <ItemTemplate>

            <tr class="TableClass">    
                <td align="left" ><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label1" Text='<%# Eval("Benutzername") %>' runat="server" OnClientClick="OpenChild()" /></td>
                <td align="left"><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label2" Text='<%# Eval("eMail") %>' runat="server" OnClientClick="OpenChild()" /></td>
                <td align="left"><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label3" Text='<%# Eval("Vorname") %>' runat="server" OnClientClick="OpenChild()" /></td>
                <td align="left"><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label4" Text='<%# Eval("Nachname") %>' runat="server" OnClientClick="OpenChild()" /></td>
                <td align="left"><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label5" Text='<%# Eval("Telefonnummer") %>' runat="server" OnClientClick="OpenChild()" /></td>

              <td align="left"><asp:Label ID="Label6" Text='<%# Eval("GUID") %>' runat="server" Visible="False" /></td>

            </tr>

        </ItemTemplate>

        <AlternatingItemTemplate>

            <tr class="TableClass"> 

                <td align="left" ><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label1" Text='<%# Eval("Benutzername") %>' runat="server" OnClientClick="OpenChild()" /></td>
                <td align="left"><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label2" Text='<%# Eval("eMail") %>' runat="server" OnClientClick="OpenChild()" /></td>
                <td align="left"><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label3" Text='<%# Eval("Vorname") %>' runat="server" OnClientClick="OpenChild()" /></td>
                <td align="left"><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label4" Text='<%# Eval("Nachname") %>' runat="server" OnClientClick="OpenChild()" /></td>
                <td align="left"><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label5" Text='<%# Eval("Telefonnummer") %>' runat="server" OnClientClick="OpenChild()" /></td>

                <td align="left"><asp:Label ID="Label6" Text='<%# Eval("GUID") %>' runat="server" Visible="False" /></td>

            </tr>

        </AlternatingItemTemplate>

        </asp:ListView>

C#代码:

...

protected void Button1_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                //Der Index der Zeile wird ermitellt
                int index = Convert.ToInt32(e.CommandArgument);

                //Der Inhalt (GUID[Unsichtbar]) wird aus der zeile mit dem Index herausgezogen und übergeben
                Label lb = (Label)myListView.Items[index].FindControl("Label6");

                //Der GUID, Benutzer-Nachname und das Werk werden an eine Session übergeben
                Session["UserID"] = lb.Text;
                Session["SearchUser"] = txtBenutzer.Text;
                Session["DropDownValue"] = dropWerk.SelectedValue;


                //Es wird eine neue Webform geöffnet
                Response.Redirect("Benutzer.aspx",true);

            }

        }

...

我希望我在会话中保存我的值而不是打开对话框

塔拉索夫

2 个答案:

答案 0 :(得分:1)

如果我正确理解你的问题,是的你可以使用javascript服务器端和ClientScriptManager.RegisterStartupScript方法这里有一篇msdn文章详细解释它

http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx

答案 1 :(得分:0)

Javascript代码总是在服务器端代码之前被调用..你必须考虑替代方式...你可以使用List View的ItemCommand属性来设置会话值,然后从Codebehind调用Javascript函数来打开弹出窗口。

protected void ListView_OnItemCommand(object sender, ListViewCommandEventArgs e)
  {
    if (String.Equals(e.CommandName, "OpenPopup"))
    {

      ListViewDataItem dataItem = (ListViewDataItem)e.Item;
      Session["UserID"]=((Label)dataItem.FindControl("Label6")).Text;
      Session["SearchUser"] = txtBenutzer.Text;                     
      Session["DropDownValue"] = dropWerk.SelectedValue;
      Page.ClientScript.RegisterStartupScript(GetType(), "Key", "OpenPopupFunction();", true); 

    }     

}