2013-04-15 4 views
0

DropDownList의 너비를 가져 오려고하고 있는데, 그 끝에 "px"가 계속 이어집니다. 최종 결과는 DropDownList의 모든 항목의 너비를 결정하여 프로그래밍 방식으로 너비를 설정할 수 있습니다.INT 형식의 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; 
    } 

어떤 아이디어 : 여기

내가 가진 무엇인가?

+0

실제 너비는 클라이언트에서만 사용할 수 있습니다. –

+0

당신은 클라이언트 측에서 이것을 위해 약간의 Javascript/CSS를 사용하고 싶을 것입니다. 서버 코드는 프리젠 테이션 레이어가 무엇을하고 있는지 알지 못합니다 (글꼴 크기 등) – geedubb

답변

0

코드에 많은 문제가 있습니다.

유형은 WebControl.Width.Value이어야합니다.

((DropDownList)sender).Items 유형은 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; 
} 
0

내가 across..Why가의 bussiness 층에서 프리젠 테이션 레이어 기능을 사용해 온 한 최악의 논리는,이 같은 뭔가 ... 이것은 당신이 서버 코드로 구현하려고하는 desinging 문제가 무엇입니까? 당신은 디자인 끝에 폭을 설정할 수 있습니다.

관련 문제