
asp.net简单投票系统问题
用的软件是VS2008,下面是代码:protectedvoidPage_Load(objectsender,EventArgse){SqlConnectionconn=n...
用的软件是VS2008,下面是代码:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data source=localhost;Initial catalog=webvotedb;Persist security info=true;user id=sa");
string sqlstr = "select * from votes";
SqlDataAdapter sda = new SqlDataAdapter(sqlstr, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "votes");
CheckBoxList1.DataSource = ds;
CheckBoxList1.DataTextField = "item";
CheckBoxList1.DataValueField = "voteid";
CheckBoxList1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string idstr = "0";
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
{
idstr += "," + li.Value;
}
}
idstr += ",0";
SqlConnection conn = new SqlConnection("Data source=localhost;Initial catalog=webvotedb;Persist security info=true;user id=sa");
string sqlstr = string.Format("update votes set votecount=votecount+1 where voteid in({0})", idstr);
SqlCommand cmd = new SqlCommand(sqlstr, conn);
conn.Open();
int i = cmd.ExecuteNonQuery();
conn.Close();
if (i > 0)
{
test.Class1.aler("投票成功!", "WebOnlinVote.aspx");
}
else
{
test.Class1.aler("投票失败!", "WebOnlinVote.aspx");
}
}
test是自己定义的一个命名空间,其作用是弹出提示窗口。上面这段代码运行时老弹出投票失败,也就是没有更新到数据库,为什么?知道的帮忙看看。谢谢啦~~~
上面的问题已经解决,Page_Load里少了if (!IsPostBack)
想问下面这个问题:如何使网页中图片的宽度等于数据库中某一列的大小?
下面的代码是gridview里的一段,用的软件是vs2008:请问如何修改下面的( Width="100px" )才能达到要求?
<asp:TemplateField HeaderText="所占百分比" SortExpression="perc">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("perc") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="20px" Align="Left"
ImageUrl="~/Images/vote.gif" Width="100px" />
<asp:Label ID="Label1" runat="server" Text='<%# Bind("perc") %>'></asp:Label>
%
</ItemTemplate>
</asp:TemplateField> 展开
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data source=localhost;Initial catalog=webvotedb;Persist security info=true;user id=sa");
string sqlstr = "select * from votes";
SqlDataAdapter sda = new SqlDataAdapter(sqlstr, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "votes");
CheckBoxList1.DataSource = ds;
CheckBoxList1.DataTextField = "item";
CheckBoxList1.DataValueField = "voteid";
CheckBoxList1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string idstr = "0";
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
{
idstr += "," + li.Value;
}
}
idstr += ",0";
SqlConnection conn = new SqlConnection("Data source=localhost;Initial catalog=webvotedb;Persist security info=true;user id=sa");
string sqlstr = string.Format("update votes set votecount=votecount+1 where voteid in({0})", idstr);
SqlCommand cmd = new SqlCommand(sqlstr, conn);
conn.Open();
int i = cmd.ExecuteNonQuery();
conn.Close();
if (i > 0)
{
test.Class1.aler("投票成功!", "WebOnlinVote.aspx");
}
else
{
test.Class1.aler("投票失败!", "WebOnlinVote.aspx");
}
}
test是自己定义的一个命名空间,其作用是弹出提示窗口。上面这段代码运行时老弹出投票失败,也就是没有更新到数据库,为什么?知道的帮忙看看。谢谢啦~~~
上面的问题已经解决,Page_Load里少了if (!IsPostBack)
想问下面这个问题:如何使网页中图片的宽度等于数据库中某一列的大小?
下面的代码是gridview里的一段,用的软件是vs2008:请问如何修改下面的( Width="100px" )才能达到要求?
<asp:TemplateField HeaderText="所占百分比" SortExpression="perc">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("perc") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="20px" Align="Left"
ImageUrl="~/Images/vote.gif" Width="100px" />
<asp:Label ID="Label1" runat="server" Text='<%# Bind("perc") %>'></asp:Label>
%
</ItemTemplate>
</asp:TemplateField> 展开
1个回答
展开全部
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn = new SqlConnection("Data source=localhost;Initial catalog=webvotedb;Persist security info=true;user id=sa");
string sqlstr = "select * from votes";
SqlDataAdapter sda = new SqlDataAdapter(sqlstr, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "votes");
CheckBoxList1.DataSource = ds;
CheckBoxList1.DataTextField = "item";
CheckBoxList1.DataValueField = "voteid";
CheckBoxList1.DataBind();
}
}
这样就可以了,
应为页面回发服务器以后又会执行一次代码,加载事件的代码再次被执行,你之前选的就会消失,下面判断的时候就会一个都没选中,就这样了
{
if (!IsPostBack)
{
SqlConnection conn = new SqlConnection("Data source=localhost;Initial catalog=webvotedb;Persist security info=true;user id=sa");
string sqlstr = "select * from votes";
SqlDataAdapter sda = new SqlDataAdapter(sqlstr, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "votes");
CheckBoxList1.DataSource = ds;
CheckBoxList1.DataTextField = "item";
CheckBoxList1.DataValueField = "voteid";
CheckBoxList1.DataBind();
}
}
这样就可以了,
应为页面回发服务器以后又会执行一次代码,加载事件的代码再次被执行,你之前选的就会消失,下面判断的时候就会一个都没选中,就这样了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询