请教一下,在超链接click事件发生的同时怎么样实现jS将前台的参数传到后台
解决方案 »
- 请大家介绍个美观实用的验证码控件?
- 用鼠标来控制div的隐藏和显示·默认div是应藏的
- 如何用FileUpload控件打开记事本并读取里面的值显示在一个文本框里 急!!!!
- 请教,关于GridView中超链接的问题
- 标题不能很好的把内容描述出来,详细内容见文章内容
- select (cInvStd+cinvname) as cinvname from inventory
- 关于从Datagrid导出execl文件
- 关于二维数组到DropDownList的绑定
- 关于存储过程传参数的一个小问题
- 用asp.net(vb)如何连接Access和SQL SERVER数据库的?
- 带xml的Flash播放器
- frameset的问题
这个相当于一个隐形的label 你可以用js将数据写入hidden中,然后因为有runat=server 所以后台也可以通过id感应到,不过这种方法对于大数据量的东西还是不行,所以如果是传递大数据还是使用ajax比较好
MyService.cs[System.Web.Script.Services.ScriptService]
public class MyService : System.Web.Services.WebService {
[WebMethod]
public string GetString(string s) {
return "Hello World " + s; //也就是给客户端传来的加个Hello World再返回给客户端。
}
}
Default.aspx<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function callService(s) {
MyService.GetString(s, OnSuccess);
}
function OnSuccess(result) {
$get('result').innerHTML = result;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/MyService.asmx" />
</Services>
</asp:ScriptManager>
<div>
<select id="sel">
<option value="1">商品一</option>
<option value="2">商品二</option>
<option value="3">商品三</option>
</select>
<input type="button" onclick="callService($get('sel').value);" value="Go!" />
</div>
<div id="result">
</div>
</form>
</body>
</html>点击Go!按钮时就把<select>中选中项的value传给服务器端,返回值显示到id=result的div内。
如:把上面button注释掉,
<%--<input type="button" onclick="callService($get('sel').value);" value="Go!" />--%>
<a href="http://www.sina.com.cn" onclick="callService($get('sel').value);">Go!</a>
点击Go!时跳转到新浪网的同时把sel选中的值传给Web Service并存入数据库.给MyService增加一个Save2Db的方法.
[WebMethod]
public void Save2Db(string s)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into Table1 values(@PName)";
cmd.Parameters.AddWithValue("PName", s);
conn.Open();
cmd.ExecuteNonQuery(); }
}这个数据库呢自己建.callService()方法改成:
function callService(s) {
//MyService.GetString(s, OnSuccess);
MyService.Save2Db(s);
}点击链接会发现网页跳转到新浪网的之前, 已经执行了把sel.value存入数据库的动作.
<asp:datalist id="DataList1" runat="server">
<a href="http://www.sina.com.cn" onclick='<%get()%>'><%# DataBinder.Eval(Container.DataItem,"CustomerID")%></a>
</asp:datalist>
datalist里面有几条数据,他就调用后台的get()几次,请问这是什么原因
datalist是一行一行进行绑定的。所以数据源有几行,你的get就要执行几次。
<Services>
<asp:ServiceReference Path="~/MyService.asmx" />
</Services>
</asp:ScriptManager>
<asp:DataList ID="DataList1" runat="server" DataKeyField="CustomerID"
DataSourceID="SqlDataSource1" onitemdatabound="DataList1_ItemDataBound">
<ItemTemplate>
CustomerID:
<asp:Label ID="CustomerIDLabel" runat="server"
Text='<%# Eval("CustomerID") %>' />
<br />
CompanyName:
<asp:DropDownList ID="ddl" runat="server">
<asp:ListItem Value="0">项目 0</asp:ListItem>
<asp:ListItem Value="1">项目 1</asp:ListItem>
<asp:ListItem Value="2">项目 2</asp:ListItem>
<asp:ListItem Value="3">项目 3</asp:ListItem>
</asp:DropDownList>
<asp:HyperLink ID="Hyperlink1" runat="server" Text='<%# Eval("CompanyName") %>' NavigateUrl="http://www.sina.com.cn" /> <br />
<br />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CustomerID], [CompanyName] FROM [Customers]">
</asp:SqlDataSource>读取Northwind的Customer表的CustomerID, CompanyName。加了个DropDownList,还有个Hyperlink.
后台:protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HyperLink link = e.Item.FindControl("Hyperlink1") as HyperLink;
DropDownList ddl = e.Item.FindControl("ddl") as DropDownList;
link.Attributes.Add("onclick",
string.Format("insert('{0}');", ddl.ClientID));
}
}也就是说给每一个Hyperlink的onclick生成如下的客户端代码:
<a id="DataList1_ctl36_Hyperlink1" onclick="insert('DataList1_ctl36_ddl');" href="http://www.sina.com.cn">Hungry Coyote Import Store</a><a id="DataList1_ctl38_Hyperlink1" onclick="insert('DataList1_ctl38_ddl');" href="http://www.sina.com.cn">Island Trading</a>
至于insert方法是调用服务器端WebService的js方法<script type="text/javascript">
function insert(id)
{
alert($get(id).value);
MyService.Insert2Db($get(id).value); //Web服务名.Web服务方法
}
</script>
//Web服务MyService.cs[System.Web.Script.Services.ScriptService]
public class MyService : System.Web.Services.WebService { public MyService () { //如果使用设计的组件,请取消注释以下行
//InitializeComponent();
} [WebMethod]
public void Insert2Db(int id)
{
using (SqlConnection conn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("insert into Table1(CustomerID, InsertTime) values(@id,@t)", conn);
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@t", DateTime.Now.ToShortDateString());
conn.Open();
cmd.ExecuteNonQuery();
}
} 其实一点都不复杂,关键还是DataList的ItemDataBound里给insert方法提供当前绑定项里的DropDownList.ClientID这一段。
可以去掉,测试用的。