2013-02-21 67 views
1

我對這個問題感到抱歉,但不知何故我有一個停電(我知道它不是藉口),但我怎麼能從RadNumbericTextBox檢索值使用按鈕發送值(我需要一個INT)如何從RadNumericTextBox檢索值

<div class="fromRowDiv"> 
    <asp:Label ID="label1" CssClass="fromLabel" runat="server" Text="From Date" ></asp:Label> 
    <telerik:RadNumericTextBox ID="fromYear" runat="server" MinValue="2000" NumberFormat-DecimalDigits="0" NumberFormat-GroupSeparator="" ></telerik:RadNumericTextBox> 
    <asp:Label ID="Tolabel2" CssClass="fromLabel" runat="server" Text="To Date"></asp:Label> 
    <telerik:RadNumericTextBox ID="toYear" runat="server" MinValue="2000" NumberFormat-DecimalDigits="0" NumberFormat-GroupSeparator=""></telerik:RadNumericTextBox>  
</div> 

protected void CalcButton_Click(object sender, EventArgs e) 
{ 
      AnnualVacationManager mng = new AnnualVacationManager(); 

      mng.CalcForNextYear(fromYear,toYear); 
} 

我需要從RadNumbericTextBox值(動態)

public AnnualVacationManager() { 
    } 

    public void CalcForNextYear(int fromYear, int toYear) 
    { 
     IEnumerable<HtUser> allUsers = HtUser.GetAll(); 
     List<AnnualVacation> newAnnualVacations = new List<AnnualVacation>(); 
     if (allUsers.Any()) { 
      foreach (HtUser user in allUsers) { 
       //int fromYear = 2013; 
       //int toYear = 2014; 
       IEnumerable<AnnualVacation> usersCurrentAnnualVacation = new List<AnnualVacation>(user.AnnualVacations.Where(a =>a.FromDate.Value.Year >= fromYear && a.FromDate.Value.Year < toYear)); 
       if (usersCurrentAnnualVacation.Any()) { 
        foreach (AnnualVacation existing in usersCurrentAnnualVacation) { 
         AnnualVacation newAnnualVacation = new AnnualVacation(); 
         //Year stuff 
         DateTime newFromDate = existing.FromDate.Value; 
         newFromDate.AddYears(toYear - fromYear); 
         DateTime newToDate = existing.ToDate.Value; 
         newToDate.AddYears(toYear - fromYear); 
         // 
         newAnnualVacation.FromDate = newFromDate; 
         newAnnualVacation.ToDate = newToDate; 
         newAnnualVacation.WorkingTime = existing.WorkingTime; 
         newAnnualVacation.VacationDays = existing.VacationDays + (existing.VacationDays - user.GetBookedVacation(fromYear)); 

         newAnnualVacations.Add(newAnnualVacation); 

         newAnnualVacation.HtUser = user; 
        } 
       } 
      } 

      HtEntityFactory.Context.SaveChanges(); 
     } 
    } 
} 

}

+0

我在答案上非常堅實,直到我看到您的文章(動態)中的最後一個單詞。 「動態」是什麼意思? – 2013-02-21 13:29:45

+0

只是不是靜態,以獲得從RadNumericTextBox鍵入的值 – Mingebag 2013-02-21 13:35:07

回答

2

的RadNumericTextBox對服務器端返回一個可爲空的小數(或也許雙)與所述用戶輸入的數值的Value屬性。要獲取價值,提供已提供一個有效的值,你這樣做:

//First .Value returns the nullable decimal or double 
//second .Value gets the decimal or double in non-nullable form 
fromYear.Value.Value 

在客戶端,它應該有一個get_value()方法得到它在:

var box = $find("<%= fromYear.ClientID %>"); 
var val = box.get_value(); 

您可能需要對於細節以特定格式的數量,因此see this格式化到如何可以調整數字的輸入,具體有:

<NumberFormat DecimalDigits="0" /> 

供應的值,以該方法爲:

mng.CalcForNextYear(Convert.ToInt32(fromYear.Value.Value), 
    Convert.ToInt32(toYear.Value.Value)); 

而且應該解決問題。你必須在RadNumericTextBox控件中有一個值才能工作。否則,請先檢查null。

+0

所以mng.CalcForNextYear(fromYear.Value.Value,toYear.Value.Value); ? – Mingebag 2013-02-21 13:29:02

+0

是的,要獲得不可爲空的十進制或雙精度值 – 2013-02-21 13:30:05

+0

除非你想以可爲空的形式(意思是double?),你可以使用一個'.Value'。 – 2013-02-21 13:30:26

0
int fromyearvalue = convert.toint32(fromYear.text) 
int toyearvalue = convert.toint32(toYear.text) 

mng.CalcForNextYear(fromyearvalue ,toyearvalue); 

OR

mng.CalcForNextYear(= convert.toint32(fromYear.totext)),convert.toint32(toYear.totext)); 
+0

我剛剛得到更多的錯誤... – Mingebag 2013-02-21 13:30:11