Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 试图获取INT格式的DropDownList的宽度_C#_Asp.net_Html_.net - Fatal编程技术网

C# 试图获取INT格式的DropDownList的宽度

C# 试图获取INT格式的DropDownList的宽度,c#,asp.net,html,.net,C#,Asp.net,Html,.net,我试图得到DropDownList的宽度,它总是以“px”结尾。我追求的最终结果是确定DropDownList中所有项目的宽度,这样我就可以通过编程设置宽度 以下是我所拥有的: public static int FindTheWidth(object sender) { DropDownList senderComboBox2 = (DropDownList)sender; // int width = senderComb

我试图得到DropDownList的宽度,它总是以“px”结尾。我追求的最终结果是确定DropDownList中所有项目的宽度,这样我就可以通过编程设置宽度

以下是我所拥有的:

    public static int FindTheWidth(object sender)
    {
        DropDownList senderComboBox2 = (DropDownList)sender;
        //            int width = senderComboBox.Width;

        var s = senderComboBox2.Width;
        var CBW = s.SubString(0, s.IndexOf("p"));
        int width2 = Convert.ToInt32(CBW);
        int vertScrollBarWidth = 2;

        int newWidth;
        foreach (string s in ((DropDownList)sender).Items)
        {
            newWidth = width2 + vertScrollBarWidth;
            if (width2 < newWidth)
            {
                width2 = newWidth;
            }
        }
        senderComboBox2.Width = width2;
        return width2;
    }
public static int FindTheWidth(对象发送方)
{
DropDownList senderCombox2=(DropDownList)发送方;
//int width=senderComboBox.width;
var s=发送方通讯录2.宽度;
var CBW=s.子串(0,s.索引f(“p”));
int width2=转换为32(CBW);
int vertScrollBarWidth=2;
int newWidth;
foreach((DropDownList)sender.Items中的字符串s)
{
newWidth=width2+vertScrollBarWidth;
如果(宽度2<新宽度)
{
宽度2=新宽度;
}
}
SenderCombox2.宽度=宽度2;
返回宽度2;
}

有什么想法吗?

代码有很多铸造问题

WebControl.Width.Value的类型应加倍

((DropDownList)发件人的类型。
项应为ListItemCollection

public static int FindTheWidth(object sender)
{
    DropDownList senderComboBox2 = (DropDownList)sender;
    var width = senderComboBox2.Width;

    double doubleValue = width.Value;
    int width2 = (int)doubleValue;
    int vertScrollBarWidth = 2;

    int newWidth;
    foreach (ListItem s in ((DropDownList)sender).Items)
    {
        newWidth = width2 + vertScrollBarWidth;
        if (width2 < newWidth)
        {
            width2 = newWidth;
        }
    }
    senderComboBox2.Width = width2;
    return width2;
}
public static int FindTheWidth(对象发送方)
{
DropDownList senderCombox2=(DropDownList)发送方;
变量宽度=发送方通讯录2.宽度;
double doubleValue=宽度.Value;
int width2=(int)doubleValue;
int vertScrollBarWidth=2;
int newWidth;
foreach(在((DropDownList)sender.Items中列出项s)
{
newWidth=width2+vertScrollBarWidth;
如果(宽度2<新宽度)
{
宽度2=新宽度;
}
}
SenderCombox2.宽度=宽度2;
返回宽度2;
}

我遇到过最糟糕的逻辑……为什么要在业务层尝试表示层功能,这是这样的……这是您试图在服务器代码中实现的设计问题?您可以尝试在设计端设置宽度。

真正的宽度仅在客户端可用。您可能希望在客户端使用一些Javascript/CSS。服务器代码不知道表示层在做什么(字体大小等)