我现在在做一个自助建站系统,里面用到一个DataGrid控件,DataGrid控件里面包含了DropDownList控件,TextBox 控件 
其中打开方式为DropDownList控件,显示为DropDownList控件,加密为DropDownList控件;
修改为Button控件, 删除为Button 控件,链接为Label控件DataGridID   栏目名称      链接        打开方式    显示      加密           修改    删除 1    单页图文     http://   原窗口/新窗口 显示/隐藏  加密/不加密     修改    删除
2    自定义链接   http://cc 原窗口/新窗口 显示/隐藏  加密/不加密    修改     删除
3    访客留言     http://   原窗口/新窗口 显示/隐藏  加密/不加密     修改    删除
4    新闻文章     http://    原窗口/新窗口 显示/隐藏  加密/不加密    修改    删除
5    单页图文     http://    原窗口/新窗口  显示/隐藏  加密/不加密    修改   删除
7    自定义链接   http://111 原窗口/新窗口  显示/隐藏  加密/不加密    修改   删除
8    首页                    原窗口/新窗口  显示/隐藏  加密/不加密    修改   删除如果栏目名称为'自定义链接 '  "链接"的Label控件转为TextBox控件,加密的DropDownList控件隐藏(也可以说是变为Label控件),
如果栏目名称为首页 删除的Button控件隐藏(也可以说是变为Label控件)
有没有相关源码啊?麻烦贴上来,发到我邮箱也可以:[email protected] 谢谢拉`

解决方案 »

  1.   

    在itemdatabound事件里面写代码,判断相关字段的内容,然后利用findcontrol方法定位控件根据特定条件来显示隐藏!代码网络上搜索下就可以自己写了
      

  2.   

    itemdatabound  它要求你是建立绑订列还是模版列啊?下面是我测试的代码,帮我看看,怎么不行?html:
    <asp:TemplateColumn HeaderText="显示">
    <HeaderStyle Width="10px"></HeaderStyle>
    <ItemTemplate>
    <asp:DropDownList id=Label4 runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "coOpen").ToString().Trim() %>'>
    <asp:ListItem Value="显示">显示</asp:ListItem>
    <asp:ListItem Value="隐藏">隐藏</asp:ListItem>
    </asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateColumn>aspx:
    private void DgManage_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    str="http://";  if(str=="http://")
     {
     DropDownList ddl = ((DropDownList)e.Item.Cells[5].FindControl("Label4"));
    ddl.Visible =false;
    }
        }
      

  3.   

    Re:itemdatabound 它要求你是建立绑订列还是模版列啊?下面是我测试的代码,帮我看看,怎么不行?html:
    <asp:TemplateColumn HeaderText="显示">....
    要加条件的 
    if(e.item.itemindex>=0)
    {
    str="http://";if(str=="http://")
    {
    DropDownList ddl = ((DropDownList)e.Item.Cells[5].FindControl("Label4"));
    ddl.Visible =false;
    }
    其次你就是说个不行,那么是报错还是没能实现
      

  4.   

    ddl.Visible =false; 报错 未将对象引用设置到对象的实例。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
    行 225:  {
    行 226:   DropDownList ddl = ((DropDownList)e.Item.Cells[5].FindControl("Label4"));
    行 227:   ddl.Visible =false;
    行 228: }
    行 229:
     源文件: f:\自助建站系统\columnmanage.aspx.cs    行: 227 
      

  5.   

    要加条件的 
    if(e.item.itemindex>=0)---》这个条件有么?
    {
    str="http://";if(str=="http://")
    {
    DropDownList ddl = ((DropDownList)e.Item.Cells[5].FindControl("Label4"));
    ddl.Visible =false;
    }
      

  6.   

    有 
    if(e.Item.ItemIndex>=0)
    {
    string username=Session["US_Users_Name"].ToString();
    int ID=int.Parse(((Label)e.Item.Cells[0].Controls[1]).Text); SqlConnection conn=new SqlConnection(strCon);
    SqlDataAdapter da=new SqlDataAdapter("select coType from C_column where UName='"+username+"' and ID='"+ID+"'",conn);
    DataSet ds=new DataSet();
    ds.Clear();
    da.Fill(ds,"C_column");
    DataTable dt=new DataTable();
    DataRow[] dr=new DataRow[10];
    dt=ds.Tables["C_column"];
    dr=dt.Select();
    string s=dr[0]["coType"].ToString();
    if(s=="自定义链接")
    {
    DropDownList ddl = ((DropDownList)e.Item.Cells[5].FindControl("Label4"));
    ddl.Visible =false;
    }
    }我现在想这样做,,我先把s 读出来,如果 s='自定义链接',就隐藏.
      

  7.   

    汗,你不会加个绑定列直接把这个自定义链接的字段绑定居然在itemdatabound事件里面进行查询,建立连接后居然不close,
      

  8.   

    不是, 你看 这就是coType这个字段里的内容
    coType
     单页图文  
    自定义链接
    访客留言
    新闻文章
     单页图文  
    网站首页
    自定义链接
    图文展示
      

  9.   

    ID UName taxis coName coType coLink coLinkState coOpen coState coEncrypt
    1 longdunjun 0 单页图  单页图文   http:// 显示 原窗口中打开 加密
    2 longdunjun 1 自定义链接 自定义链接 http:// 显示 原窗口中打开 加密3 longdunjun 2 4545 访客留言 http:// 加密 显示 原窗口中打开 加密4 longdunjun 0 sdfasdf 新闻文章 http:// -- 显示 原窗口中打开 加密
    5 longdunjun 0 eddd  单页图文   http:// -- 显示 原窗口中打开 加密
    6 longdunjun
    1 dd 网站首页 http:// -- 显示 原窗口中打开 加密
    7 longdunjun 0 sdfasdf 自定义链接 http://111 -- 隐藏 在原窗口中打开 不加密
    8 longdunjun 0 dsf 图文展示 http:// -- 隐藏 在原窗口中打开 不加密

    这是数据库的那个表
      

  10.   

    html:
    <TR vAlign="top" align="center">
    <TD width="200" height="418">&nbsp;</TD>
    <TD vAlign="top" width="625" height="418"><!--DWLayoutEmptyCell--><asp:datagrid id="DgManage" runat="server" AllowCustomPaging="True" AllowSorting="True" CellPadding="3"
    BorderWidth="1px" BorderColor="#999999" Font-Underline="True" GridLines="Vertical" BackColor="White" AutoGenerateColumns="False" AllowPaging="True" Width="780px"
    Height="312px">
    <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle>
    <EditItemStyle Font-Size="7pt" Font-Names="Arial" HorizontalAlign="Center" ForeColor="Black" BackColor="ScrollBar"></EditItemStyle>
    <AlternatingItemStyle Font-Size="8pt" Font-Names="Arial" HorizontalAlign="Center" BackColor="Gainsboro"></AlternatingItemStyle>
    <ItemStyle Font-Size="8pt" Font-Names="Arial" HorizontalAlign="Center" ForeColor="Black" BackColor="Silver"></ItemStyle>
    <HeaderStyle Font-Size="9pt" Font-Names="Arial" Font-Bold="True" HorizontalAlign="Center" ForeColor="White"
    BackColor="#000084"></HeaderStyle>
    <FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
    <Columns>
    <asp:TemplateColumn HeaderText="ID">
    <ItemTemplate>
    <asp:Label id="Label7" Width="50px" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ID").ToString().Trim() %>'>
    </asp:Label>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="排序">
    <HeaderStyle Width="10px"></HeaderStyle>
    <HeaderTemplate>
    <B>排序 </B>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:TextBox id=labeltaxis Width="50px" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "taxis").ToString().Trim() %>'>
    </asp:TextBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="栏目名称">
    <HeaderStyle Width="10px"></HeaderStyle>
    <ItemTemplate>
    <asp:TextBox id=Label6 Width="96px" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "coName").ToString().Trim() %>'>
    </asp:TextBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="栏目类型">
    <HeaderStyle Width="10px"></HeaderStyle>
    <ItemTemplate>
    <asp:Label id=Label1 Width="94px" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "coType").ToString().Trim() %>'>
    </asp:Label>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="链接">
    <HeaderStyle Width="10px"></HeaderStyle>
    <ItemTemplate>
    <asp:Label id=Label2 Width="114px" runat="server" Text='<%# cc(DataBinder.Eval(Container.DataItem, "coLink").ToString().Trim()) %>'>
    </asp:Label>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="打开方式">
    <HeaderStyle Width="10px"></HeaderStyle>
    <ItemTemplate>
    <asp:DropDownList id=Label3 runat="server" Width="70px" Text='<%# DataBinder.Eval(Container.DataItem, "coState").ToString().Trim() %>'>
    <asp:ListItem Value="原窗口">原窗口</asp:ListItem>
    <asp:ListItem Value="新窗口">新窗口</asp:ListItem>
    </asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="显示">
    <HeaderStyle Width="10px"></HeaderStyle>
    <ItemTemplate>
    <asp:DropDownList id=Label4 runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "coOpen").ToString().Trim() %>'>
    <asp:ListItem Value="显示">显示</asp:ListItem>
    <asp:ListItem Value="隐藏">隐藏</asp:ListItem>
    </asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="加密">
    <HeaderStyle Width="10px"></HeaderStyle>
    <ItemTemplate>
    <asp:DropDownList id="Dropdownlist1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "coEncrypt").ToString().Trim() %>'>
    <asp:ListItem Value="加密">加密</asp:ListItem>
    <asp:ListItem Value="不加密">不加密</asp:ListItem>
    </asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:EditCommandColumn ButtonType="PushButton" UpdateText="更新" HeaderText="修改" CancelText="取消" EditText="修改">
    <HeaderStyle Width="10px"></HeaderStyle>
    </asp:EditCommandColumn>
    <asp:ButtonColumn Text="删除" ButtonType="PushButton" HeaderText="删除" CommandName="Delete">
    <HeaderStyle Width="10px"></HeaderStyle>
    </asp:ButtonColumn>
    </Columns>
    <PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999" Mode="NumericPages"></PagerStyle>
    </asp:datagrid>&nbsp;</TD>
    <TD width="182" height="418">&nbsp;</TD>
    </TR>
    <TR vAlign="top">
    <TD vAlign="top" colSpan="3" height="31">
    <P>&nbsp;</P>
    </TD>
    </TR>
    <tr>
    <td colSpan="3"></td>
    </tr>
    </TBODY></TABLE>
      

  11.   

    aspx:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;namespace websystem
    {
    /// <summary>
    /// ColumnManage 的摘要说明。
    /// </summary>
    public class ColumnManage : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid DgManage;
    string UserName="";
    string strCon="server=(local);database=SB_WebSystem;uid=sa;pwd=";
    public ICollection CreateDataSource()             //数据同步
    {
    //查C_column表  存储过程
    SqlConnection conn=new SqlConnection(strCon);
    conn.Open();
    SqlCommand cmd=new SqlCommand("select_C_column1",conn);
    cmd.CommandType=CommandType.StoredProcedure; SqlDataAdapter da=new SqlDataAdapter();
    da.SelectCommand=cmd;
    DataSet ds=new DataSet();
    ds.Clear();
    da.Fill(ds,"C_column");
    DgManage.DataSource=ds.Tables["C_column"].DefaultView;
    DgManage.DataBind();
    DataView myView=ds.Tables["C_column"].DefaultView;
    return myView;                   //返回值
    conn.Close(); }
    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(Session["US_Users_Name"]==null)
    {
    Response.Redirect("Login.aspx");
    }
    else
    {
    UserName=Session["US_Users_Name"].ToString();//传Session值
    }

    if(!IsPostBack)
    {

    DgManage.DataSource=CreateDataSource();
    DgManage.DataBind();
    }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.DgManage.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DgManage_EditCommand);
    this.DgManage.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DgManage_ItemDataBound);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    //修改事件
    private void DgManage_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {

    string username=Session["US_Users_Name"].ToString();
    int ID=int.Parse(((Label)e.Item.Cells[0].Controls[1]).Text);
    int taxis=int.Parse(((TextBox)e.Item.Cells[1].Controls[1]).Text);
    string coName=((TextBox)e.Item.Cells[2].Controls[1]).Text;
    string coType=((Label)e.Item.Cells[3].Controls[1]).Text;
    DropDownList drpcoOpen=(DropDownList)e.Item.Cells[6].FindControl("Label4");
    string coOpen=drpcoOpen.SelectedItem.Value;
    DropDownList drpcoState=(DropDownList)e.Item.Cells[5].FindControl("Label3");
    string coState=drpcoState.SelectedItem.Value+"中打开";
    //string coEncrypt=((DropDownList)e.Item.Cells[6].Controls[1]).ToString();

    SqlConnection conn=new SqlConnection(strCon);

    SqlCommand cmd=new SqlCommand("update_C_column",conn);

    cmd.CommandType=CommandType.StoredProcedure;

       
    cmd.Parameters.Add("@ID",SqlDbType.Int);
    cmd.Parameters.Add("@username",SqlDbType.VarChar);
    cmd.Parameters.Add("@taxis",SqlDbType.Int);
    cmd.Parameters.Add("@coName",SqlDbType.VarChar);
    cmd.Parameters.Add("@coType",SqlDbType.VarChar);
    cmd.Parameters.Add("@coLink",SqlDbType.VarChar);
    cmd.Parameters.Add("@coOpen",SqlDbType.VarChar);
    cmd.Parameters.Add("@coState",SqlDbType.VarChar);
    cmd.Parameters.Add("@coEncrypt",SqlDbType.VarChar);


    cmd.Parameters["@ID"].Value=ID;
    cmd.Parameters["@username"].Value=username;
    cmd.Parameters["@taxis"].Value=taxis;
    cmd.Parameters["@coName"].Value=coName;
    cmd.Parameters["@coOpen"].Value=coOpen;
    cmd.Parameters["@coState"].Value=coState;



    SqlDataAdapter da=new SqlDataAdapter("select coType from C_column where UName='"+username+"' and ID='"+ID+"'",conn);
    DataSet ds=new DataSet();
    ds.Clear();
    da.Fill(ds,"C_column");
    DataTable dt=new DataTable();
    DataRow[] dr=new DataRow[10];
    dt=ds.Tables["C_column"];
    dr=dt.Select();
    string s=dr[0]["coType"].ToString();
    cmd.Parameters["@coType"].Value=s;


     

    if(s=="自定义链接")
    {

    cmd.Parameters["@coLink"].Value=((Label)(e.Item.Cells[4].FindControl("Label2"))).Text ;
    // Label l=((Label)(e.Item.Cells[4].FindControl("Label2"))).Text ;
    //l.Visible=false;
    //Response.Write("<script language='javascript'>alert('"+(e.Item.Cells[4].FindControl("Label2")).ToString()+");</script>");
    }
    else
    {
    //cmd.Parameters["@coLink"].Value="http://";
    }
    if(s=="自定义链接"||s=="网站首页")
    {
    //DropDownList drpcoEncrypt=(DropDownList)e.Item.Cells[7].FindControl("Label5");

    //string coEncrypt=drpcoEncrypt.SelectedItem.ToString();

    // cmd.Parameters["@coEncrypt"].Value=coEncrypt;
    }
    else
    {
    //DropDownList drpcoEncrypt=(DropDownList)e.Item.Cells[7].FindControl("Label5");
    //string coEncrypt=drpcoEncrypt.SelectedItem.ToString();
    //cmd.Parameters["@coEncrypt"].Value=coEncrypt;
    } try
    {
    conn.Open();
    cmd.ExecuteNonQuery();
    Response.Write("添加成功");
    DgManage.EditItemIndex=-1;
    DgManage.DataSource=CreateDataSource();
    DgManage.DataBind();

    //cmd.ExecuteNonQuery();
    conn.Close();
    }
    catch(Exception ex)
    {
    Response.Write("<script language='javascript'>alert('"+ex.Message+"');</script>");
    }
    } private void DgManage_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    /* string username=Session["US_Users_Name"].ToString();
    //int ID=int.Parse(((Label)e.Item.Cells[0].Controls[1]).Text);
    TextBox lbl= (TextBox)e.Item.Cells[0].Controls[1].FindControl("ID");
    int ID=int.Parse(lbl.Text.ToString()); if(e.Item.ItemIndex>=0)
    {
    SqlConnection conn=new SqlConnection(strCon);
    SqlDataAdapter da=new SqlDataAdapter("select coType from C_column where UName='"+username+"' and ID='"+ID+"'",conn);
    DataSet ds=new DataSet();
    ds.Clear();
    da.Fill(ds,"C_column");
    DataTable dt=new DataTable();
    DataRow[] dr=new DataRow[10];
    dt=ds.Tables["C_column"];
    dr=dt.Select();
    string s=dr[0]["coType"].ToString();
    if(s=="自定义链接")
    {
    DropDownList ddl = ((DropDownList)e.Item.Cells[5].FindControl("Label4"));
    ddl.Visible =false;
    }
    }*/
        }
    }
    }
      

  12.   

    我刚刚查了下
    string username=Session["US_Users_Name"].ToString();
    //int ID=int.Parse(((Label)e.Item.Cells[0].Controls[1]).Text);
    //TextBox lbl= (TextBox)e.Item.Cells[0].Controls[1].FindControl("ID");
    int ID=2;
    SqlConnection conn=new SqlConnection(strCon);
    SqlDataAdapter da=new SqlDataAdapter("select coType from C_column where UName='"+username+"' and ID="+ID,conn);

    DataSet ds=new DataSet();
    ds.Clear();
    da.Fill(ds,"C_column");
    DataTable dt=new DataTable();
    DataRow[] dr=new DataRow[10];
    dt=ds.Tables["C_column"];
    dr=dt.Select();
    string s=dr[0]["coType"].ToString();
                 Response.Write(s);
    返回了20条'自定义链接'请问我这代码有没有错 啊`
    怎么会出现这中情况?
      

  13.   

    你好,我现在也在做自助建站系统,可以聊聊啊,[email protected]
      

  14.   

    你的MSN是多少,可以在做的过程中讨论共同学习
      

  15.   

    在itemdatabound事件里面写代码,判断相关字段的内容,然后利用findcontrol方法定位控件根据特定条件来显示隐藏!
    Lable lbl = e.Item.FindControl("Label1"); 
    if(lbl.Text = "首页")
    {
       Button btn = (Button)e.Item.FindControl("DeleteBtn");
       btn.Visual = False;
    }
      

  16.   

    你的string username=Session["US_Users_Name"].ToString();
    //int ID=int.Parse(((Label)e.Item.Cells[0].Controls[1]).Text);
    //TextBox lbl= (TextBox)e.Item.Cells[0].Controls[1].FindControl("ID");
    int ID=2;
    SqlConnection conn=new SqlConnection(strCon);
    SqlDataAdapter da=new SqlDataAdapter("select coType from C_column where UName='"+username+"' and ID="+ID,conn);

    DataSet ds=new DataSet();
    ds.Clear();
    da.Fill(ds,"C_column");
    DataTable dt=new DataTable();
    DataRow[] dr=new DataRow[10];
    dt=ds.Tables["C_column"];
    dr=dt.Select();
    string s=dr[0]["coType"].ToString();
                 Response.Write(s);
    没有错误,但是不合理不过现在不用管这些
      

  17.   

    谢谢各位兄弟帮忙,我那问题终于解决,我用qinhl99(林鸿) 的那段代码加上jimu8130(好累好累) 的 一些技术,终于搞定
    Label lbl=(Label)e.Item.Cells[1].FindControl("Label5");

    //Response.Write(lbl.Text.ToString());
    if(e.Item.ItemIndex>=0)
    {
    if(lbl.Text=="自定义链接")
    {

    DropDownList ddl = ((DropDownList)e.Item.Cells[0].FindControl("Label4"));
    ddl.Visible =false;

    }
    }
    这样就OK拉```````````