Maskeditextender未在gridview中动态绑定到文本框 [英] Maskeditextender is not being bound to textbox dynamically in gridview
问题描述
我的gridview中有一个Masked编辑扩展器。我根据类型动态显示这个gridview,如果类型为T,则只显示文本框,如果类型为DT,则显示带日历的文本框。
我正在使用相同的文本框,并在网格行数据绑定事件中,我隐藏并显示日历按钮。但是如果我在gridview中给maskededitextender,那么掩码也会应用于非日期字段。
我尝试给出一个虚拟文本框并将其作为targetcontrolid for maskededtextender和rowdatabound事件,如果类型是日期,我将maskededitextender的targetcontrolid设置为我的文本框。但它没有用。
有人可以帮我吗?
什么我试过了:
下面是rowdatabound事件代码:
protected void grd_RowDataBound (对象发送者,GridViewRowEventArgs e)
{
尝试
{
if(e.Row.RowType = = DataControlRowType.DataRow)
{
MaskedEditExtender ME =(MaskedEditExtender)e.Row.FindControl(MaskedEditExtender1);
TextBox txtValue1 =(TextBox)e.Row.FindControl(txtValue1);
TextBox txttest =(TextBox)e.Row.FindControl(txttest);
HiddenField hfInputType =(HiddenField)e.Row.FindControl(hfInputType);
ImageButton imgBtnFromDate =(ImageButton)e.Row.FindControl(imgBtnFromDate);
ME.TargetControlID = txttest.ClientID;
if(hfInputType.Value ==DT)
{
txtValue1.Style.Add(HtmlTextWriterStyle.Display,block) ;
imgBtnFromDate.Style.Add(HtmlTextWriterStyle.Display,block);
ME.TargetControlID = txtValue1.ClientID;
}
else if(hfInputType.Value ==U)
{
txtValue1.Style。添加(HtmlTextWriterStyle.Display,block);
imgBtnFromDate.Style.Add(HtmlTextWriterStyle.Display,none);
}
}
}
}
Asp.net代码:
< asp:ImageButton ID =imgBtnFromDaterunat =serverStyle =display :noneImageUrl =〜/ App_Themes / Black / Images / Calendar.png/>
< cc1:CalendarExtender ID =ceFromDatePopupButtonID =imgBtnFromDaterunat =server
TargetControlID =txtValue1>
< cc1:MaskedEditExtender ID =MaskedEditExtender1runat =serverAcceptNegative =Left
DisplayMoney =LeftMask =99/99/9999MaskType =DateMessageValidatorTip =true
>
I have a Masked edit extender in my gridview. I am displaying this gridview dynamically based on types, If the type is "T" it displays only textbox, if the type is "DT" it displays textbox with calendar.
I am using the same textbox, and in grid row databound event, I am hiding and showing the calendar button. But if I give the maskededitextender in the gridview, the mask is being applied for the nondate fields also.
I tried by giving a dummy textbox and make it as targetcontrolid for the maskededtextender and in rowdatabound event, I am setting the targetcontrolid of the maskededitextender to my textbox if the type is date. But it is not working.
can anyone please help me on this?
What I have tried:
Below is the rowdatabound event code:
protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
MaskedEditExtender ME = (MaskedEditExtender)e.Row.FindControl("MaskedEditExtender1");
TextBox txtValue1 = (TextBox)e.Row.FindControl("txtValue1");
TextBox txttest = (TextBox)e.Row.FindControl("txttest");
HiddenField hfInputType = (HiddenField)e.Row.FindControl("hfInputType");
ImageButton imgBtnFromDate = (ImageButton)e.Row.FindControl("imgBtnFromDate");
ME.TargetControlID = txttest.ClientID;
if(hfInputType.Value=="DT")
{
txtValue1.Style.Add(HtmlTextWriterStyle.Display, "block");
imgBtnFromDate.Style.Add(HtmlTextWriterStyle.Display, "block");
ME.TargetControlID = txtValue1.ClientID;
}
else if(hfInputType.Value=="U")
{
txtValue1.Style.Add(HtmlTextWriterStyle.Display, "block");
imgBtnFromDate.Style.Add(HtmlTextWriterStyle.Display, "none");
}
}
}
}
Asp.net code:
<asp:ImageButton ID="imgBtnFromDate" runat="server" Style="display: none" ImageUrl="~/App_Themes/Black/Images/Calendar.png" />
<cc1:CalendarExtender ID="ceFromDate" PopupButtonID="imgBtnFromDate" runat="server"
TargetControlID="txtValue1">
<cc1:MaskedEditExtender ID="MaskedEditExtender1" runat="server" AcceptNegative="Left"
DisplayMoney="Left" Mask="99/99/9999" MaskType="Date" MessageValidatorTip="true"
>
推荐答案
删除此行
remove this line
ImageButton imgBtnFromDate = (ImageButton)e.Row.FindControl("imgBtnFromDate");
ME.TargetControlID = txttest.ClientID; // this is global scope, it will be applied to all controls.
if(hfInputType.Value=="DT")
{
这篇关于Maskeditextender未在gridview中动态绑定到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!