点击按钮时将datalist控件中的值取出来提交,现在的问题是能取到datalist里面的控件但是不能取到控件的值。调试发现值是空的。
解决方案 »
- 网站静态化的问题。思想?方法?
- 如何在google地图中显示数据库中的坐标?
- 求教大家一个问题sqlhelper类型无法转换
- 我的分都在这了,希望各位帮帮忙!
- 100分的问题,大家看看,这个20分就算送大家的了
- 结帐,好了,感谢morefish(little),结帐啦
- 谁能给我介绍本比较好的页面美工设计的书(要电子版的)?谢谢(顶就有分)
- 如何判断http://100.100.100.100/folder/file.txt是否存在?
- .net读取excel一个单元格中的内容
- 怎么在aspx上动态加载web user control?
- 一个关于GridView和DataList的问题(在线等待)
- 点开一个下拉列表,鼠标一滑动它就消失了
{
//数据绑定
}
{
TextBox txtSendTo1 = (TextBox)d.FindControl("txtSendTo");
string txtsendTovalue=txtSendTo1.Text;
}
发现为空值在提交前txtSendTo有内容的,注意提交的按钮并不在dtSmallTask的模板内。所以ItemCommand之类的东西没考虑。请高手指教
{}这个存在的控件是找得到的但是就是取不到模板里面控件的值
用findcontrol取不到?
demo
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandSource.GetType() == typeof(Button))
{
if (((Button)e.CommandSource).CommandName == "accept")
{
if (((CheckBox)e.Item.FindControl("select")).Checked)
{ int id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString());
int stuid = Convert.ToInt32(((Label)e.Item.FindControl("label1")).Text);
string stuname = ((Label)e.Item.FindControl("label2")).Text;
string stuadd = ((Label)e.Item.FindControl("label3")).Text;
SqlConnection cn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("insert into s(StuID,StuName,Stuaddurl) values('" + stuid + "','" + stuname + "','" + stuadd + "')", cn);
cn.Open();
cmd.ExecuteNonQuery();
SqlCommand cmd1 = new SqlCommand("delete from students where ID='" + id + "'", cn);
cmd1.ExecuteNonQuery();
cn.Close();
bind();
}
}
if (((Button)e.CommandSource).CommandName == "noaccept")
{
if (((CheckBox)e.Item.FindControl("select")).Checked)
{
int id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString());
int stuid = Convert.ToInt32(((Label)e.Item.FindControl("label1")).Text);
string stuname = ((Label)e.Item.FindControl("label2")).Text;
string stuadd = ((Label)e.Item.FindControl("label3")).Text;
SqlConnection cn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("insert into sj(stuid,stuname,stuadd) values('" + stuid + "','" + stuname + "','" + stuadd + "')", cn);
cn.Open();
cmd.ExecuteNonQuery();
SqlCommand cmd1 = new SqlCommand("delete from students where ID='" + id + "'", cn);
cmd1.ExecuteNonQuery();
cn.Close();
bind();
}
}
}
对模板里面控件存在值。但是我提交的按钮并不在datalist这个控件里面也就是说我点击按钮的作用是循环找datalist里面控件的值并对数据进行数据库操作。至于ItemCommand事件应该是触发不到的。
<%@ Page Language="C#" AutoEventWireup="true" EnableViewState ="false" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<table>
<asp:TextBox ID="txt" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox>
</table>
</ItemTemplate>
</asp:DataList>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Name"));
dt.Rows.Add(new object[] { "River" });
dt.Rows.Add(new object[] { "China" });
DataList1.DataSource = dt;
DataList1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (DataListItem item in DataList1.Items)
{
TextBox tb = (TextBox)item.FindControl("txt");
Response.Write(tb.Text);
}
}
}
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="border-collapse:collapse;" border=1>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<tr>
<td>
<%#Eval("EmpID") %>
</td>
<td>
<asp:TextBox ID="txtName" runat="server" Text=<%#Eval("EmpName")%>></asp:TextBox>
</td>
</tr>
</ItemTemplate>
</asp:DataList>
</table>
<asp:Button ID="Button1" runat="server" Text="提交" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
public class Employee
{
public string EmpID { set; get; }
public string EmpName { set; get; }
} protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.DataList1.DataSource = new System.Collections.Generic.List<Employee>()
{
new Employee(){ EmpID="S001", EmpName="张三"},
new Employee(){ EmpID="S002", EmpName="李四"},
new Employee(){ EmpID="S003", EmpName="王五"},
new Employee(){ EmpID="S004", EmpName="赵六"},
new Employee(){ EmpID="S005", EmpName="小七"}
};
this.DataList1.DataBind();
}
} protected void Button1_Click(object sender, EventArgs e)
{
string message = string.Empty;
foreach (DataListItem item in this.DataList1.Items)
{
TextBox tx = item.FindControl("txtName") as TextBox;
if (!string.IsNullOrEmpty(tx.Text))
{
message += tx.Text + "|";
}
}
Response.Write(message.TrimEnd('|')); //输出信息
}
{
foreach (DataListItem d in dtSmallTask.Items)
{
TextBox txtSendTo = (TextBox)d.FindControl("txtSendTo");
TextBox hdnTxtSendTo = (TextBox)d.FindControl("hdnTxtSendTo");
TextBox txtCcTo = (TextBox)d.FindControl("txtCcTo");
TextBox hdnTxtCcTo = (TextBox)d.FindControl("hdnTxtCcTo");
TextBox txtBccTo = (TextBox)d.FindControl("txtBccTo");
TextBox hdnTxtBccTo = (TextBox)d.FindControl("hdnTxtBccTo");
TextBox txtstartTime = (TextBox)d.FindControl("txtstartTime");
TextBox txtendTime = (TextBox)d.FindControl("txtendTime");
CheckBox ckman1 = (CheckBox)d.FindControl("ckman1");
CheckBox ckman2 = (CheckBox)d.FindControl("ckman2");
CheckBox ckman3 = (CheckBox)d.FindControl("ckman3");
CheckBox ckman4 = (CheckBox)d.FindControl("ckman4");
CheckBox ckmsg1 = (CheckBox)d.FindControl("ckmsg1");
CheckBox ckmsg2 = (CheckBox)d.FindControl("ckmsg2");
CheckBox ckmsg3 = (CheckBox)d.FindControl("ckmsg3");
CheckBox CheckBox1 = (CheckBox)d.FindControl("CheckBox1");
CheckBox CheckBox2 = (CheckBox)d.FindControl("CheckBox2");
CheckBox CheckBox3 = (CheckBox)d.FindControl("CheckBox3");
TextBox txtTaskContent = (TextBox)d.FindControl("txtTaskContent");
SqlParameter[] para =
{
new SqlParameter("@ZongRenWuBianHao",txtAllTaskID.Text ),
new SqlParameter("@RenWuBianHao",txtAllTaskName.Text ),
new SqlParameter("@FuZeRen",txtSendTo.Text ),
new SqlParameter("@StrFuZeRen",hdnTxtSendTo.Text ),
new SqlParameter("@XieBanRen",txtCcTo.Text ),
new SqlParameter("@StrXieBanRen",hdnTxtCcTo.Text ),
new SqlParameter("@CanYueRen",txtBccTo.Text ),
new SqlParameter("@StrCanYueRen",hdnTxtBccTo.Text ),
new SqlParameter("@KaiShiShiJian",txtstartTime.Text ),
new SqlParameter("@JieShuShiJian",txtendTime.Text ),
new SqlParameter("@TiXingFangShi1", ckman1.Checked == true ? "1" : "0"),
new SqlParameter("@TiXingFangShi2", ckman2.Checked == true ? "1" : "0"),
new SqlParameter("@TiXingFangShi3", ckman3.Checked == true ? "1" : "0"),
new SqlParameter("@TiXingFangShi4", ckman4.Checked == true ? "1" : "0"),
new SqlParameter("@FaSongFangShi1",ckmsg1.Checked == true ? "1" : "0"),
new SqlParameter("@FaSongFangShi2",ckmsg2.Checked == true ? "1" : "0"),
new SqlParameter("@FaSongFangShi3",ckmsg3.Checked == true ? "1" : "0"),
new SqlParameter("@TiXingSheZhi1",CheckBox1.Checked == true ? "1" : "0"),
new SqlParameter("@TiXingSheZhi2",CheckBox2.Checked == true ? "1" : "0"),
new SqlParameter("@TiXingSheZhi3",CheckBox3.Checked == true ? "1" : "0"),
new SqlParameter("@RenWuLeiRong",""),
new SqlParameter("@FuJian","")
}; bs.Execute("Proc_Zong_FenRenWu_InsertORUpdate2", para);
}
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; border-right: #3399cc 2px dashed; border-top: #3399cc 2px dashed; border-left: #3399cc 2px dashed; border-bottom: #3399cc 2px dashed;">
<tr>
<td class="ItemTitleFont" style="width: 114px; height: 30px;">
<strong>
会务分项:</strong></td>
<td style="width: 536px; height: 30px;">
<asp:TextBox ID="txtSmallTaskNM" runat="server" CssClass="InputStyle" Height="18px"
TabIndex="1"
Width="230px" ReadOnly="True" Text='<%# Eval("RenWuMingCheng") %>'></asp:TextBox>
<asp:TextBox ID="txtSmallTaskID" runat="server" CssClass="InputStyle" Height="18px" ReadOnly="True" TabIndex="1" Width="52px" Text='<%# Eval("RenWuBianHao") %>'></asp:TextBox>
<asp:TextBox ID="rowitemname" runat="server" Text="<%#(Container).ItemIndex + 1%>" CssClass="InputStyle" Height="18px" Width="84px" Visible="False"></asp:TextBox></td>
</tr>
<tr>
<td class="ItemTitleFont" style="width: 114px; height: 36px;">
负责人:</td>
<td style="width: 536px; height: 36px;">
<asp:TextBox ID="txtSendTo" runat="server" CssClass="InputStyle" Height="30px"
TabIndex="1" TextMode="MultiLine" Width="292px" value="<%=SendToRealName%>" name="txtSendTo" ></asp:TextBox>
<a style="CURSOR: hand" onclick="dialwinprocess(<%#(Container).ItemIndex%>)" href="javascript:void(0);">添加人员</a><asp:TextBox ID="hdnTxtSendTo" runat="server"
CssClass="InputStyle" Height="18px" TabIndex="1" TextMode="MultiLine" Width="154px" value="<%=SendTo%>" name="hdnTxtSendTo" Text='<%# Eval("FuZeRen") %>'></asp:TextBox></td>
</tr>
<tr>
<td class="ItemTitleFont" style="width: 114px; height: 30px;">
协办人:</td>
<td style="width: 536px; height: 30px;">
<asp:TextBox ID="txtCcTo" runat="server" CssClass="InputStyle" Height="30px" TabIndex="1" TextMode="MultiLine" Width="292px" value="<%=CcToRealName%>" name="txtCcTo" Text='<%# Eval("StrXieBanRen") %>'></asp:TextBox>
<a onclick="window.open('AddMenber.aspx', 'newwindow', 'height=520, width=515, top=150, left=400, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no')"><span
style="text-decoration: underline; cursor: hand;"></span></a><asp:TextBox ID="hdnTxtCcTo" runat="server"
CssClass="InputStyle" Height="18px" TabIndex="1" TextMode="MultiLine" Width="154px" value="<%=CcTo%>" name="hdnTxtCcTo" Text='<%# Eval("XieBanRen") %>'></asp:TextBox></td>
</tr>
<tr>
<td class="ItemTitleFont" style="width: 114px; height: 30px;">
参阅人:</td>
<td style="width: 536px; height: 30px;">
<asp:TextBox ID="txtBccTo" runat="server" CssClass="InputStyle" Height="30px" TabIndex="1" TextMode="MultiLine" Width="292px" value="<%=BccToRealName%>" name="txtBccTo" Text='<%# Eval("StrCanYueRen") %>' ></asp:TextBox>
<a onclick="window.open('AddMenber.aspx', 'newwindow', 'height=520, width=515, top=150, left=400, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no')"><span
style="text-decoration: underline; cursor: hand;"><asp:TextBox ID="hdnTxtBccTo" runat="server"
CssClass="InputStyle" Height="18px" TabIndex="1" TextMode="MultiLine" Width="154px" value="<%=BccTo%>" name="hdnTxtBccTo" Text='<%# Eval("CanYueRen") %>'></asp:TextBox></span></a></td>
</tr>
<tr>
<td class="ItemTitleFont" style="width: 114px; height: 30px">
任务时间设置:</td>
<td style="width: 536px; height: 30px">
<asp:TextBox ID="txtstartTime" runat="server" CssClass="inputdate" Height="16px" name="txtstartTime" onclick="setday(this)" ReadOnly="True" TabIndex="3" Width="154px" Text='<%# Eval("KaiShiShiJian") %>'></asp:TextBox>
至
<asp:TextBox ID="txtendTime" runat="server" CssClass="inputdate" Height="16px" name="txtendTime" onclick="setday(this)" ReadOnly="True" TabIndex="3" Width="154px" Text='<%# Eval("JieShuShiJian") %>'></asp:TextBox></td>
</tr>
<tr style="color: #004779">
<td class="ItemTitleFont" style="width: 114px; height: 30px">
提醒接受人设置:</td>
<td style="width: 536px; height: 30px" valign="middle"><asp:CheckBox ID="ckman1" runat="server" Text="发布人" Checked='<%# Eval("TiXingFangShi1").ToString()=="1" ? true:false %>' />
<asp:CheckBox ID="ckman2" runat="server" Text="责任人" Checked='<%# Eval("TiXingFangShi2").ToString()=="1" ? true:false %>' />
<asp:CheckBox ID="ckman3" runat="server" Text="协办人" Checked='<%# Eval("TiXingFangShi3").ToString()=="1" ? true:false %>' />
<asp:CheckBox ID="ckman4" runat="server" Text="参阅人" Checked='<%# Eval("TiXingFangShi4").ToString()=="1" ? true:false %>' /> </table>
</ItemTemplate>
</asp:DataList>
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; border-right: #3399cc 2px dashed; border-top: #3399cc 2px dashed; border-left: #3399cc 2px dashed; border-bottom: #3399cc 2px dashed;">
<tr>
<td class="ItemTitleFont" style="width: 114px; height: 30px;">
<strong>
会务分项:</strong></td>
<td style="width: 536px; height: 30px;">
<asp:TextBox ID="txtSmallTaskNM" runat="server" CssClass="InputStyle" Height="18px"
TabIndex="1"
Width="230px" ReadOnly="True" Text='<%# Eval("RenWuMingCheng") %>'></asp:TextBox>
<asp:TextBox ID="txtSmallTaskID" runat="server" CssClass="InputStyle" Height="18px" ReadOnly="True" TabIndex="1" Width="52px" Text='<%# Eval("RenWuBianHao") %>'></asp:TextBox>
<asp:TextBox ID="rowitemname" runat="server" Text="<%#(Container).ItemIndex + 1%>" CssClass="InputStyle" Height="18px" Width="84px" Visible="False"></asp:TextBox></td>
</tr>
<tr>
<td class="ItemTitleFont" style="width: 114px; height: 36px;">
负责人:</td>
<td style="width: 536px; height: 36px;">
<asp:TextBox ID="txtSendTo" runat="server" CssClass="InputStyle" Height="30px"
TabIndex="1" TextMode="MultiLine" Width="292px" value="<%=SendToRealName%>" name="txtSendTo" ></asp:TextBox>
<a style="CURSOR: hand" onclick="dialwinprocess(<%#(Container).ItemIndex%>)" href="javascript:void(0);">添加人员</a><asp:TextBox ID="hdnTxtSendTo" runat="server"
CssClass="InputStyle" Height="18px" TabIndex="1" TextMode="MultiLine" Width="154px" value="<%=SendTo%>" name="hdnTxtSendTo" Text='<%# Eval("FuZeRen") %>'></asp:TextBox></td>
</tr>
<tr>
<td class="ItemTitleFont" style="width: 114px; height: 30px;">
协办人:</td>
<td style="width: 536px; height: 30px;">
<asp:TextBox ID="txtCcTo" runat="server" CssClass="InputStyle" Height="30px" TabIndex="1" TextMode="MultiLine" Width="292px" value="<%=CcToRealName%>" name="txtCcTo" Text='<%# Eval("StrXieBanRen") %>'></asp:TextBox>
<a onclick="window.open('AddMenber.aspx', 'newwindow', 'height=520, width=515, top=150, left=400, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no')"><span
style="text-decoration: underline; cursor: hand;"></span></a><asp:TextBox ID="hdnTxtCcTo" runat="server"
CssClass="InputStyle" Height="18px" TabIndex="1" TextMode="MultiLine" Width="154px" value="<%=CcTo%>" name="hdnTxtCcTo" Text='<%# Eval("XieBanRen") %>'></asp:TextBox></td>
</tr>
<tr>
<td class="ItemTitleFont" style="width: 114px; height: 30px;">
参阅人:</td>
<td style="width: 536px; height: 30px;">
<asp:TextBox ID="txtBccTo" runat="server" CssClass="InputStyle" Height="30px" TabIndex="1" TextMode="MultiLine" Width="292px" value="<%=BccToRealName%>" name="txtBccTo" Text='<%# Eval("StrCanYueRen") %>' ></asp:TextBox>
<a onclick="window.open('AddMenber.aspx', 'newwindow', 'height=520, width=515, top=150, left=400, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no')"><span
style="text-decoration: underline; cursor: hand;"><asp:TextBox ID="hdnTxtBccTo" runat="server"
CssClass="InputStyle" Height="18px" TabIndex="1" TextMode="MultiLine" Width="154px" value="<%=BccTo%>" name="hdnTxtBccTo" Text='<%# Eval("CanYueRen") %>'></asp:TextBox></span></a></td>
</tr>
<tr>
<td class="ItemTitleFont" style="width: 114px; height: 30px">
任务时间设置:</td>
<td style="width: 536px; height: 30px">
<asp:TextBox ID="txtstartTime" runat="server" CssClass="inputdate" Height="16px" name="txtstartTime" onclick="setday(this)" ReadOnly="True" TabIndex="3" Width="154px" Text='<%# Eval("KaiShiShiJian") %>'></asp:TextBox>
至
<asp:TextBox ID="txtendTime" runat="server" CssClass="inputdate" Height="16px" name="txtendTime" onclick="setday(this)" ReadOnly="True" TabIndex="3" Width="154px" Text='<%# Eval("JieShuShiJian") %>'></asp:TextBox></td>
</tr>
<tr style="color: #004779">
<td class="ItemTitleFont" style="width: 114px; height: 30px">
提醒接受人设置:</td>
<td style="width: 536px; height: 30px" valign="middle"><asp:CheckBox ID="ckman1" runat="server" Text="发布人" Checked='<%# Eval("TiXingFangShi1").ToString()=="1" ? true:false %>' />
<asp:CheckBox ID="ckman2" runat="server" Text="责任人" Checked='<%# Eval("TiXingFangShi2").ToString()=="1" ? true:false %>' />
<asp:CheckBox ID="ckman3" runat="server" Text="协办人" Checked='<%# Eval("TiXingFangShi3").ToString()=="1" ? true:false %>' />
<asp:CheckBox ID="ckman4" runat="server" Text="参阅人" Checked='<%# Eval("TiXingFangShi4").ToString()=="1" ? true:false %>' /> </table>
</ItemTemplate>
</asp:DataList>
你按如下方法给多行文本框赋值 protected void dtSmallTask_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
TextBox tb = (TextBox)e.Item.FindControl("txtSendTo");
tb.Text = ((DataRowView)e.Item.DataItem)["Name"].ToString();
}
}
TabIndex="1" TextMode="MultiLine" Width="292px" value="<%=SendToRealName%>" name="txtSendTo" ></asp:TextBox>value="<%=SendToRealName%>" 不要用Value了,用Text
控件都取不到值,还是检查下配置文件有没有做全局enableviewstate设置,并在本页面page命令加上 EnableViewState="true"另外,其它页面没有这样的问题?