动态控制DataGrid中的TextBox的状态及输入值!!

要实现的功能是:
   1. TextBox "领用数量" 是动态生成的.
   2. 在后台动态控制"领用数量"的状态.
   3. 控制"领用数量"的输入量不能大于"可用数量" .
       如果输入后的"领用数量"会大于"可用数量",则无法输入!!! 
       直接脚本实现,省掉了提交,检查,回传,弹出对话框等等一系列繁琐的工作.

如图:



前台:
<asp:datagrid id="dgXimu" runat="server" DataKeyField="TID" OnUpdateCommand="update" OnCancelCommand="cancel"
    OnEditCommand
="edit" AutoGenerateColumns="False" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3"
    Width
="100%" AllowPaging="True">
    
<SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle>
    
<AlternatingItemStyle BackColor="White"></AlternatingItemStyle>
    
<ItemStyle ForeColor="Black" BackColor="White"></ItemStyle>
    
<HeaderStyle Font-Bold="True" ForeColor="#336699" BackColor="#E6EEF7"></HeaderStyle>
    
<FooterStyle ForeColor="#336699" BackColor="#E6EEF7"></FooterStyle>
    
<Columns>
        
<asp:BoundColumn DataField="WhinNo" ReadOnly="True" HeaderText="入仓单号">
            
<HeaderStyle Width="25%"></HeaderStyle>
        
</asp:BoundColumn>
        
<asp:BoundColumn DataField="WhinDate" ReadOnly="True" HeaderText="入仓时间">
            
<HeaderStyle Width="25%"></HeaderStyle>
        
</asp:BoundColumn>
        
<asp:BoundColumn DataField="WareName" HeaderText="库别">
            
<HeaderStyle Width="10%"></HeaderStyle>
        
</asp:BoundColumn>
        
<asp:BoundColumn DataField="QtyKy" HeaderText="可用数量">
            
<HeaderStyle Width="15%"></HeaderStyle>
        
</asp:BoundColumn>
        
<asp:BoundColumn HeaderText="领用数量">
            
<HeaderStyle Width="10%"></HeaderStyle>
        
</asp:BoundColumn>
    
</Columns>
</asp:datagrid>


添加一段Javascript:
<script language="javascript">
    
function isInt(obj,strQtyKy)
    {
        
if(event.keyCode<48 || event.keyCode>57)
        {
            event.keyCode
=0;
        }
        
else
        {
            
//原来的领用数量
            var QtyLyOld
            
if(obj.value=="")
                QtyLyOld 
= 0;
            
else
                QtyLyOld 
= obj.value;
            
            
//按下的数量
            var QtyLyNew
                QtyLyNew 
= parseInt(event.keyCode)-48;

            
if((QtyLyOld + QtyLyNew) > parseInt(strQtyKy))
            {
                event.keyCode
=0;
            }            
        }                
    }                    
</script>


后台:
private void dgXimu_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
    
try
    {
        OASys.Public.Public objPub
=new OASys.Public.Public();
        
for(int i=0; i < dgXimu.Items.Count; i++)
        {
            
//主键列
            string strTID = dgXimu.DataKeys[i].ToString();

            
//可用数量列
            TableCell cellQtyKy = dgXimu.Items[i].Cells[4];
            
string strQtyKy = cellQtyKy.Text;

            
//领用数量列
            TableCell cellQtyLy    = dgXimu.Items[i].Cells[5];
            cellQtyLy.Controls.Clear();
        
            
//领用数量列 - 控制ReadOnly
            TextBox tb  = new TextBox();
            tb.CssClass 
= "input_text";
            tb.Width 
= 80;
            tb.MaxLength 
= 10;
            tb.EnableViewState 
= true;
            tb.Attributes.Add(
"onkeypress",@"isInt(this,"+ strQtyKy +")");
            cellQtyLy.Controls.Add(tb);
        }
        objPub.Close();            
    }
    
catch(Exception ex)
    {
        
throw ex;
    }
}
原文地址:https://www.cnblogs.com/tohen/p/757227.html