带有提交按钮和执行命令的按钮的 c# asp.net 表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11991120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:59:25  来源:igfitidea点击:

c# asp.net Form with a submit button and a button that executes a command?

c#asp.netbuttonform-submit

提问by JazziJeff

basically i have a form with the runat server tag. within this form i have an asp repeater and asubmit button- but i also want to have a button which will call a c# method in my code - can someone please give me an idea as to how to go about this? Also in this code the selectindexchanged event does not fire even though its autopostback value is true which is weird :/

基本上我有一个带有 runat 服务器标签的表单。在这个表单中,我有一个 asp 中继器和一个提交按钮——但我也想要一个按钮,它会在我的代码中调用 ac# 方法——有人可以告诉我如何去做吗?同样在此代码中,即使其 autopostback 值为 true,selectindexchanged 事件也不会触发,这很奇怪:/

thanks

谢谢

EditOK solution seems to be remove the action tab and i have then set the property through some JavaScript instead :) why cant things be straightforward

编辑OK 解决方案似乎是删除操作选项卡,然后我通过一些 JavaScript 设置了属性:) 为什么事情不能直截了当



    <form action="broadcast.aspx" id="bcForm" runat="server" onsubmit="chkChecks()"> 
            <a class="btn btn-warning" href="#" title="Create new product"> <i class="icon-plus icon-white"></i> Create new product</a> &nbsp &nbsp <a id="bcItems" data-toggle="modal" class="btn btn-primary" title="Broadcast" onclick="chkItems()"><i class="icon-share icon-white"></i> Broadcast</a>

</br></br>
<p>Filters:
<asp:DropDownList ID="groupFilter" runat="server" ToolTip="Groups"        AutoPostBack="True" onselectedindexchanged="groupFilter_SelectedIndexChanged1" ></asp:DropDownList></p>


   <div>
        <asp:Repeater ID="DepartmentList" runat="server"  OnItemmDataBound="DepartmentsList_ItemDataBound">
   <HeaderTemplate>
         <table id="grouptable" class="table table-bordered table-striped sortable"> 
                    <thead>
                            <tr>
                            <th class="sorttable_nosort" >Broadcast 
                                <br> </br><a class="label label-inverse" onclick="checkAll()">Check All</a>&nbsp<a class="label label-inverse" onclick="uncheckAll()">Uncheck All</a></th>
                            <th class="sorttable_numeric">ID</th>
                            <th>Name</th>
                            <th>Last Modified</th>
                            <th>Actions</th>
                        </tr>
                        </thead>
                        <tbody>
                </HeaderTemplate>
                <ItemTemplate>
                            <tr>
                            <td WIDTH="100">
                            <input type="checkbox" name="BroadcastSelect[]" value="<%# Eval("DEPT_ID") %>"</input></td>
                            <td>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("DEPT_ID") %>'></asp:Label></td>
                            <td><%# Eval("DESC") %></td>
                            <td>
                                <asp:Label ID="Label2" runat="server" Text='<%# Eval("CHG_DATE") %>'></asp:Label></td>
                            <td><a class="btn btn-info" href="<%# Eval("gURL") %>">Edit</a>&nbsp;&nbsp<asp:HyperLink ID="HyperLink1" runat="server" CssClass="btn btn-danger">Broadcast Now</asp:HyperLink></td>
                            </tr>
                </ItemTemplate>
                <FooterTemplate>
                            </tbody>
                    </table>
                </FooterTemplate>
                </asp:Repeater>
                </div>



              <div id="myModal" class="modal hide fade" runat="server">
                <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal">&times; Select A Branch</h3>
                </div>
                <div class="modal-body">
                  <h4>Test</h4>

                <asp:Repeater ID="cfgList" runat="server">
                    <HeaderTemplate>
                   <table id="configtable" class="table table-bordered table-striped"> 
                    <thead>
                            <tr>
                            <th>Broadcast</th>
                            <th>Branch No.</th>
                            <th>Branch Name</th>
                        </tr>
                        </thead>
                        <tbody>
                </HeaderTemplate>
                <ItemTemplate>
                            <tr>
                            <td>
                            <input type="checkbox" name="BranchSelection[]" value="<%# Eval("branch") %>"</input></td>
                            <td><%# Eval("branch") %></td>
                            <td><%# Eval("name") %></a> </td>
                            </tr>
                </ItemTemplate>
                <FooterTemplate>
                            </tbody>
                    </table>
                </FooterTemplate>
                </asp:Repeater>


                </div>

                <div class="modal-footer">
                  <!--<a class="btn btn-success" onclick="document.getElementById('bcForm').submit()" title="Broadcast"><i class="icon-share icon-white"></i> Broadcast</a> -->
                  <a class="btn btn-success" onclick="chkBranches()" title="Broadcast"><i class="icon-share icon-white"></i> Broadcast</a>
                  <!--<input type="submit" value="Broadcast3" class="btn btn-success"/> -->
                  <div id="myDiv"></div>
                </div>
              </div>
              <!-- <a data-toggle="modal" href="#myModal" class="btn btn-primary">Broadcast Testing</a> -->
                <!--<a class="btn btn-success" onclick="document.getElementById('bcForm').submit()" title="Broadcast"><i class="icon-share icon-white"></i> Broadcast</a>-->
                <a id="bcItems2" data-toggle="modal" class="btn btn-primary" title="Broadcast" onclick="chkItems()"><i class="icon-share icon-white"></i> Broadcast</a>
                <ul class="pager">
                <li>
                <asp:Button ID="previousBtn" runat="server" Text="Prev" cssclass="search-query" 
                        Width="80px"/>
                </li>
                <li>
                <asp:Button ID="nextBtn" runat="server" Text="Next" cssclass="search-query" 
                        Width="80px"/>
                </li>
                </ul>
                </form>

采纳答案by JazziJeff

remove the action attribute first from the form. – DotNet Dreamer

首先从表单中删除 action 属性。– 网络梦想家

this was basically the answer, check the original post which i have updated with the fix :)

这基本上就是答案,请查看我已使用修复程序更新的原始帖子:)

props to DotNet Dreamer

DotNet Dreamer 的道具

回答by Oblivion2000

I must be missing something in your question, because this isn't difficult, but here you go...

我一定在你的问题中遗漏了一些东西,因为这并不难,但是你去吧......

HTML:

HTML:

<asp:Button runat="server" Text="Whatever" OnClick="SomeMethod_Click"/>

C# code behind

后面的 C# 代码

public void SomeMethod_Click(object sender, EventArgs e);

回答by Larry

In the dropdownlist property, you can set AutoPostBack = true. With this it will send the post back to server therefore event can be triggered.

在下拉列表属性中,您可以设置 AutoPostBack = true。有了这个,它会将帖子发送回服务器,因此可以触发事件。

oh. sry i didn't see you already have it

哦。对不起,我没有看到你已经拥有了

回答by Zac

Example:

例子:

<script runat="server">
Sub submit(sender As Object, e As EventArgs)
lbl1.Text="Your name is " & txt1.Text
End Sub
</script>

<html>
<body>

<form runat="server">
Enter your name:
<asp:TextBox id="txt1" runat="server" />
<asp:Button OnClick="submit" Text="Submit" runat="server" />
<p><asp:Label id="lbl1" runat="server" /></p>
</form>

</body>
</html>

link for help: http://www.w3schools.com/aspnet/aspnet_button.asp

帮助链接:http: //www.w3schools.com/aspnet/aspnet_button.asp