我有一个DATAGRID,里面有一百列,我要根据内容设置列宽,如何动态设置列宽,或者动态生成模板列?我试过在HTML里面加for()代码块,却报错说不用在此位置加代码块。请教各位高手!

解决方案 »

  1.   

    private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
    {
    string[] color = {"#FF0000"
     ,"#00FF00"
     ,"#0000FF"
     ,"#FFFF00"
     ,"#00FFFF"
     ,"#FF00FF"}; string[] img = { "bar0.gif"
       ,"bar1.gif"
       ,"bar2.gif"
       ,"bar3.gif"
       ,"bar4.gif"
       ,"bar5.gif"
       ,"bar6.gif"
       ,"bar7.gif"
       ,"bar8.gif"
       ,"bar9.gif"
       }; HtmlGenericControl option = (HtmlGenericControl)e.Item.FindControl("option");
    Image imgoption = (Image)e.Item.FindControl("imgoption"); Label Label_Count = (Label)e.Item.FindControl("Label_Count");
    int option_count = int.Parse(Label_Count.Text);
    float width; if(option_count<1)
    {
    width = 0.00000001F;
    }
    else
    {
    width = float.Parse(Label_Count.Text)/Count;
    } if(BackImg)
    {
    imgoption.ImageUrl = Globals.GetSkinPath() + "/Images/Vote/" + img[r.Next(img.Length)];
    imgoption.Width = (int)(300*width);
    option.Visible = false;
    }
    else
    {
    option.Attributes["width"] = (300*width).ToString();
    option.Attributes["style"] = "background-color:" + color[r.Next(color.Length)] + ";";
    imgoption.Visible = false;
    }
    Label_Count.Text = option_count.ToString() + "票,占" + width.ToString("P");
    }
      

  2.   

    大哥,Image 是什么???
      

  3.   

    大哥,能不能详细点。imgoption是一个什么控件。那些.gif是些什么图片。BackImg又什么?我刚做开发,请哥哥说详细点
      

  4.   

    <ASP:TEMPLATECOLUMN FooterText="newvalue" HeaderText="货物名称">
    <HEADERSTYLE Width="20%" HorizontalAlign="Center"></HEADERSTYLE>
    <ITEMSTYLE HorizontalAlign="Center"></ITEMSTYLE>
    <ITEMTEMPLATE>
               <%# DataBinder.Eval(Container.DataItem, "GoodsID", "{0}") %>
    </ITEMTEMPLATE>
    <FOOTERSTYLE HorizontalAlign="Center"></FOOTERSTYLE>
    <FOOTERTEMPLATE>
    <ASP:TEXTBOX id="txtGoodsID" runat="server" cssclass="Text" MaxLength="30" Visible="false"></ASP:TEXTBOX>
    </ASP:TEMPLATECOLUMN>中的: Width="20%"