我有两个控件,一个是TextBox,另一个是radcombobox
in a ItemTemplate of a GridView。我想要做的是当radcombobox
的event onclientsideselectedindexchanges
被触发时。我想用JavaScript在客户端显示/隐藏TextBox。
这样做的基本目的是避免回发以显示将被DataBound到数据库的TextBox。
如果不可能在客户端做,那么请在服务器端建议一些替代方案。
发布于 2012-05-17 17:49:27
在我的示例中,我有一个带有模板字段的GridView,它包含一个DropDown菜单和一个TextBox。JavaScript中的OnLoad
事件将TextBox的显示样式设置为"none"
。设置GridView的DropDown in OnRowDataBound
事件的OnChange
事件,并调用JavaScript函数,将TextBox显示样式设置为您需要的样式。
在我的示例中,仅当DropDown选择的索引为"1"
时才显示TextBox。
我已经在我的代码中完成了所有这些-参考这个: Gridview ID="GridView1"
<Columns>
<asp:TemplateField HeaderText="Order Status">
<ItemTemplate>
<asp:DropDownList ID="ddlOrderStatus" runat="server" Width="104px" ToolTip="Select order status">
</asp:DropDownList><br />
<asp:TextBox ID="txtShipTrackNo" runat="server"
Width="100px" Text='<%# Eval("sct_order_docket_id") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<script type="text/javascript">
var TargetBaseControl = null;
window.onload = function() {
try {
//get target base control.
TargetBaseControl = document.getElementById('<%= this.GridView1.ClientID %>');
for (var rowId = 1; rowId < TargetBaseControl.rows.length; ++rowId) {
var txtShip = TargetBaseControl.rows[rowId].cells[0].getElementsByTagName("input")[0];
txtShip.style.display = "none";
}
}
catch (err) {
TargetBaseControl = null;
}
}
function selectCheck(rowid)
{
TargetBaseControl = document.getElementById('<%= this.GridView1.ClientID %>');
var ddlStatus = TargetBaseControl.rows[rowid].cells[0].getElementsByTagName("select")[0];
var txtShip = TargetBaseControl.rows[rowid].cells[0].getElementsByTagName("input")[0];
txtShip.style.display = "none";
if (ddlStatus.selectedIndex == '1') {
txtShip.style.display = "block";
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlStatus = (DropDownList)e.Row.FindControl("ddlOrderStatus");
string strFunc = string.Format("return selectCheck('{0}')", e.Row.RowIndex + 1);
ddlStatus.Attributes.Add("onchange", strFunc);
}
}
https://stackoverflow.com/questions/10625948
复制相似问题