控制Gridview每行内的一些控件互斥

Magic_YJL 2008-06-16 02:50:06
GridView每行里都有一个TextBox,一个DropDownList,
那么我在对某一行操作的时候,向该行的TextBox里输入了数据,就不能从该行的DropDownList里选择数据了,
相反,如果我在该行的DropDownList里选择了数据,那我就不能在TextBox里面输入数据了。
怎么实现?
望大家给个思路,或者源码。
...全文
146 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Magic_YJL 2008-06-16
  • 打赏
  • 举报
回复
小弟 十分非常感谢大家~
大自然D使者 2008-06-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hubblebubblepig 的回复:]
引用 1 楼 hubblebubblepig 的回复:
当textbox内的text不为""时 则DropDownList.Enabled = false;
为""则相反
当DropDownList的选项发生变化时 则textbox.Enabled = false;
若选中默认项(在DropDownList中加一默认项 "请选择"之类的) 则相反

分别在TextChanged和selectedchanged事件处理程序里面进行判断
并且还要在进行这些判断之前获取行号以定位具体是哪两个控件
[/Quote]
keyake863 2008-06-16
  • 打赏
  • 举报
回复
楼主对以上回复 满意?
nghf102 2008-06-16
  • 打赏
  • 举报
回复
每天都学习一些
fellowcheng 2008-06-16
  • 打赏
  • 举报
回复
//前台页面

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlSelect" runat="server" onchange="EnableOne(2);">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:DropDownList>
<input type="text" ID="txtInput" runat="server" onchange="EnableOne(1);"/>
</ItemTemplate>

</asp:TemplateField>
</Columns>
</asp:GridView>


 <script type="text/javascript">
function EnableOne(model)
{
var ele = event.srcElement;
var parent = ele.parentElement;
var ddls = parent.getElementsByTagName("select");
var txts = parent.getElementsByTagName("input");
// ele.disabled = false;
if(model == 1)
{
ddls[0].disabled = true;
}
if(model == 2)
{
txts[0].disabled = true;
txts[0].value = ddls[0].options[ddls[0].selectedIndex].innerText;
}
}
</script>
amandag 2008-06-16
  • 打赏
  • 举报
回复
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
function elementChange(id)
{
document.getElementById(id).disabled = true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="0">aa</asp:ListItem>
<asp:ListItem Value="1">bb</asp:ListItem>
<asp:ListItem Value="2">cc</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;uid=sa;pwd=;" ProviderName="System.Data.SqlClient" SelectCommand="select top 10 productid from products" DataSourceMode="DataReader"></asp:SqlDataSource>
</form>
</body>
</html>


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList drop;
TextBox txt;
if (e.Row.RowType == DataControlRowType.DataRow)
{
drop = e.Row.FindControl("DropDownList1") as DropDownList;
txt = e.Row.FindControl("TextBox1") as TextBox;

drop.Attributes.Add("onchange", "elementChange('" + txt.ClientID + "')");
txt.Attributes.Add("onchange", "elementChange('" + drop.ClientID + "')");
}
}
amandag 2008-06-16
  • 打赏
  • 举报
回复
肯定是用js脚本控制好
talentxiaoli 2008-06-16
  • 打赏
  • 举报
回复
..楼上的楼上..
写这个的时候..没看到有楼上
talentxiaoli 2008-06-16
  • 打赏
  • 举报
回复
不赞成楼上的说法~~
最好用JS脚本 写入TEXT的时候让 DROP 选择到你之前没有选的那一项(通常人们都用把第一项设为:请选择)
当 DROP 的值改变了的时候 就把TEXT 的innerHTML值清空~~
原理应该跟GridView的无刷新全选差不多,网上找下代码
fellowcheng 2008-06-16
  • 打赏
  • 举报
回复
直接用js吧
hubblebubblepig 2008-06-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hubblebubblepig 的回复:]
当textbox内的text不为""时 则DropDownList.Enabled = false;
为""则相反
当DropDownList的选项发生变化时 则textbox.Enabled = false;
若选中默认项(在DropDownList中加一默认项 "请选择"之类的) 则相反
[/Quote]
分别在TextChanged和selectedchanged事件处理程序里面进行判断
并且还要在进行这些判断之前获取行号以定位具体是哪两个控件
hubblebubblepig 2008-06-16
  • 打赏
  • 举报
回复
当textbox内的text不为""时 则DropDownList.Enabled = false;
为""则相反
当DropDownList的选项发生变化时 则textbox.Enabled = false;
若选中默认项(在DropDownList中加一默认项 "请选择"之类的) 则相反

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧