本文介绍了Asp.net,将文本框值与数据表列值进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello开发人员,

我想将文本框值与会话中创建的数据表进行比较。如果文本框值与数据表列项ID匹配,则不应允许在按钮单击时重新输入该项。



我尝试了什么:



protected void AddInGrid(object sender,EventArgs e)

{



messagelabel.Text =;

DataTable dtCurrentTable =(DataTable)ViewState [CurrentTabele];

DataRow drCurrentRow = null;



if(dtCurrentTable.Rows.Count> 0& txtquantity.Text!=0& checkstock()== true)

{



for(int i = 1; i< = dtCurrentTable.Rows.Count; i ++)

{



//创建新行并分配值

drCurrentRow = dtCurrentTable.NewRow();

drCurrentRow [Item Id] = Convert.ToIn t32(txtid.Text);

drCurrentRow [Item Name] = txtname.Text;

drCurrentRow [Price] = Convert.ToInt32(txtprice.Text) ;

drCurrentRow [Quantity] = Convert.ToInt32(txtquantity.Text);

drCurrentRow [Item Total] = Convert.ToInt32(txttotalitemprice.Text);

}

//删除初始空行

if(dtCurrentTable.Rows [0] [0] .ToString()== )

{

dtCurrentTable.Rows [0]。删除();

dtCurrentTable.AcceptChanges();



}





//在DataTable中添加新记录

dtCurrentTable.Ro ws.Add(drCurrentRow);

//将DataTable存储到ViewState

ViewState [CurrentTabele] = dtCurrentTable;

//绑定Gridview与新行

gvGridview1.DataSource = dtCurrentTable;

gvGridview1.DataBind();



}





否则if(checkstock()== false)

{

messagelabel .Text =股票不够;

}







}

Hello developers,
I want to compare textbox value with data table created in the session. If the textbox value matches with data table column item id, it should not allow to re enter that item on button click. Screenshot

What I have tried:

protected void AddInGrid(object sender, EventArgs e)
{

messagelabel.Text = "";
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTabele"];
DataRow drCurrentRow = null;

if (dtCurrentTable.Rows.Count > 0 & txtquantity.Text != "0" & checkstock()==true)
{

for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{

//Creating new row and assigning values
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["Item Id"] = Convert.ToInt32(txtid.Text);
drCurrentRow["Item Name"] = txtname.Text;
drCurrentRow["Price"] = Convert.ToInt32(txtprice.Text);
drCurrentRow["Quantity"] = Convert.ToInt32(txtquantity.Text);
drCurrentRow["Item Total"] = Convert.ToInt32(txttotalitemprice.Text);
}
//Removing initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.AcceptChanges();

}


//Added New Record to the DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//storing DataTable to ViewState
ViewState["CurrentTabele"] = dtCurrentTable;
//binding Gridview with New Row
gvGridview1.DataSource = dtCurrentTable;
gvGridview1.DataBind();

}


else if (checkstock() == false)
{
messagelabel.Text = "Stock Not Enough";
}



}

推荐答案

//try to find the item id
DataRow[] drs = dtCurrentTable.Select("Item Id = '" + yourtextbox.Text + "'");

//Verify if the select return one line 
if (drs.Length > 0)
{
    //item finded, exit without add line
    messagelabel.Text = "This data already exists. You can't add again.";
    return;                    
}





这里!完整的代码。



Here! The complete code.

protected void AddInGrid(object sender, EventArgs e)
{

messagelabel.Text = "";
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTabele"];

/****************************************************************
* CODE ADDED                                                    *
****************************************************************/

//try to find the item id
DataRow[] drs = dtCurrentTable.Select("Item Id = '" + yourtextbox.Text + "'");

//Verify if the select return one line 
if (drs.Length > 0)
{
    //item finded, exit without add line
    messagelabel.Text = "This data already exists. You can't add again.";
    return;                    
}

/****************************************************************
* END CODE                                                      *
****************************************************************/

//Your code

DataRow drCurrentRow = null;

if (dtCurrentTable.Rows.Count > 0 & txtquantity.Text != "0" & checkstock()==true)
{

for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{

//Creating new row and assigning values
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["Item Id"] = Convert.ToInt32(txtid.Text);
drCurrentRow["Item Name"] = txtname.Text;
drCurrentRow["Price"] = Convert.ToInt32(txtprice.Text);
drCurrentRow["Quantity"] = Convert.ToInt32(txtquantity.Text);
drCurrentRow["Item Total"] = Convert.ToInt32(txttotalitemprice.Text);
}
//Removing initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.AcceptChanges();

}


//Added New Record to the DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//storing DataTable to ViewState
ViewState["CurrentTabele"] = dtCurrentTable;
//binding Gridview with New Row
gvGridview1.DataSource = dtCurrentTable;
gvGridview1.DataBind();

}


else if (checkstock() == false)
{
messagelabel.Text = "Stock Not Enough";
}

}


这篇关于Asp.net,将文本框值与数据表列值进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 09:10