asp.net gridview绑定了第三方控件ColorPicker一列,但是点击该控件选择颜色赋值时却只能赋值到第一行,除了第一行之外的赋值不上怎么办??
代码如下:
protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ModifyButton")
{
int i =Convert.ToInt32( e.CommandArgument.ToString());
System.Web.UI.WebControls.TextBox tb = GridView3.Rows[i].FindControl("tbname") as System.Web.UI.WebControls.TextBox;
string statusName = tb.Text;
HiddenField hf = GridView3.Rows[i].FindControl("hiddenColor") as HiddenField;
HiddenField hf1 = GridView3.Rows[i].FindControl("hiddenId") as HiddenField;
ColorPicker cp = GridView3.Rows[i].FindControl("ColorPicker2") as ColorPicker;
string color = cp.Color;
if (statusName != string.Empty &&color!=string.Empty )
{
int id = Convert.ToInt32(hf1.Value);
string sqlupdate = string.Format("update Status set StatusName='{0}',StatusColor='{1}' where ID='{2}'", statusName, color, id);
int result = SqlHelper.ExecuteNonQuery(connectionString, System.Data.CommandType.Text, sqlupdate);
if (result > 0)
{
ClientScript.RegisterStartupScript(this.GetType(), "提示", "alert('修改成功~!')", true);
BindStatus();
TabWebControl1.SelectedTabIndex = 3;
//Response.Redirect("AdminManager.aspx");
}
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "提示", "alert('状态名和颜色不能为空~!')", true);
}
}
你应该用clientID
我怀疑是你使用了在Grid中使用了客户端ID,同时ColorPicker注册到了ID上。然后GridView渲染数据的时候,ID重复了。这个时候,ColorPicker就只能设置第一个框了。因为ID选择器只匹配第一个。你应该贴前台的GridView代码。
谢谢你!这是前台代码,你帮我看看,该怎么改,在线等你
<asp:GridView
ID="GridView3" runat="server" AutoGenerateColumns="False"
CellPadding="4" EnableModelValidation="True" ForeColor="#333333"
GridLines="None" onrowcommand="GridView3_RowCommand"><AlternatingRowStyle
BackColor="White" /><Columns><asp:TemplateField HeaderText="状态名"><ItemTemplate><asp:TextBox ID="tbname" runat="server"
Text='<%#Eval("StatusName")%>'></asp:TextBox></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="颜色"><ItemTemplate>
<cc2:ColorPicker runat="server" Color='<%#Eval("StatusColor")%>' ID="ColorPicker2" oncolorchanged="ColorPicker2_ColorChanged" /><asp:HiddenField ID="hiddenColor" runat="server"
Value='<%#Eval("StatusColor")%>' /><asp:HiddenField ID="hiddenId" runat="server" Value='<%#Eval("ID")%>' /></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="删除"><ItemTemplate><asp:ImageButton ID="ImageButton1" runat="server"
CommandArgument='<%#DataBinder.Eval(Container,"RowIndex")%>'
CommandName="deleteButton" ImageUrl="~/images/del3.png"></asp:ImageButton></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="修改"><ItemTemplate><asp:ImageButton ID="ok" runat="server"
CommandArgument='<%#DataBinder.Eval(Container,"RowIndex")%>'
CommandName="ModifyButton" ImageUrl="~/images/modify.png" /></ItemTemplate></asp:TemplateField></Columns><EditRowStyle BackColor="#2461BF" /><FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /><HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /><PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /><RowStyle BackColor="#EFF3FB" /><SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /></asp:GridView>
@lijunmomo: 不好意思,不是频繁看博文。看看ColorPicker2_ColorChanged这个方法的实现。
@幻天芒: 这个事件根本就不触发的。。。不知道怎么回事
@lijunmomo:那参考下这个控件的api。
@幻天芒: api什么意思呢?
@lijunmomo:API(Application Programming Interface,应用程序编程接口)
你就看看这个控件的demo吧。