如何使用webCont用sp列表来组合txt框值 [英] how comp txt box value with sp list using webCont
问题描述
Code.ascs.cs:文件
受保护的 无效 btn_Submit_Click(对象发件人,EventArgs e)
{
lbl_EmpCode.Text = " ;
SPSite新闻站点= SPContext.Current.Site;
SPWeb网站= newsites.OpenWeb();
SPQuery查询= 新 SPQuery();
query.Query = 字符串 .Format(" + < Where>" + < Eq>" + + " + txt_Companycode.Text + </Value>" + " + " </何处>" + </Query>");
SPList list = webs.Lists [" ];
SPListItemCollection items = list.GetItems(query);
如果(项目.计数> 0 )
{
txt_Companycode.Text = " ;
Response.Redirect(" );
}
其他
{
lbl_EmpCode.Text = " " ;
Response.Redirect(" );
}
}
以下是我的.ascx文件
<%@ 控制 语言 =" C#" AutoEventWireup true" CodeBehind =" 继承 Company_Code.Company_Code " %>
< 表 =" style1" < tr >
< td =" style2" < asp:Label ID =" runat 服务器" 字体粗体 True" ForeColor ="
span> 文本 输入国家/地区代码" < >
< /td >
< td =" style3" < asp:TextBox ID =" runat 服务器" > /asp:TextBox >
< /td >
< td >
< asp:Label ID =" runat 服务器" ForeColor #FF0066" < /asp:Label >
< /td >
< /tr >
< tr >
< td =" style2" & nbsp; < /td >
< td =" style3" < asp:Button ID =" runat 服务器" 字体粗体 True" ForeColor ="
文本 =" =" 宽度 =" 67像素" / >
< /td >
< td >
< asp:RequiredFieldValidator ID =" runat 服务器"
ErrorMessage ="
ControlToValidate =" > < /asp: RequiredFieldValidator >
< /td >
< /tr >
< /table >
此代码有什么问题?当我在文本框中输入输入内容时,它会提示我错误.我正在将文本框的值与列表Item进行比较,如果匹配,则转到下一页,否则返回错误并保留在同一页面中.
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用Microsoft.SharePoint;
使用System.IO;
使用System.Web.Configuration;
命名空间UserControls
{
公共局部类CheckCompanyCode:System.Web.UI.UserControl
{
公共字符串siteUrl;
受保护的void Page_Load(对象发送者,EventArgs e)
{}
受保护的无效btn_submit_Click(对象发送者,EventArgs e)
{
字符串companyCode = txt_companyCode.Text.ToString().Trim();
试试
{
SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();
SPContext currentContext = SPContext.Current;
SPList myList = myWeb.Lists ["Companies"];
SPQuery myQuery =新的SPQuery();
myQuery.Query = @< Where>< Eq>< FieldRef名称=''Company_x0020_Code''/><值类型=''文本''>""
+ companyCode
+</Value></Eq></Where>";
SPListItemCollection项目= myList.GetItems(myQuery);
如果(items.Count == 1)
{
Response.Redirect(〜/Pages/ClaimAccount.aspx?EmployerCode =" + companyCode);
}
其他
{
lbl_EmpCode.Text =公司代码与现有记录不匹配..!";
}
}
catch(ex ex例外)
{
Response.Write(< br>< br>" + ex.Message.ToString());
}
}
}
}
Code.ascs.cs: File
protected void btn_Submit_Click(object sender, EventArgs e)
{
lbl_EmpCode.Text = "";
SPSite newsites = SPContext.Current.Site;
SPWeb webs = newsites.OpenWeb();
SPQuery query = new SPQuery();
query.Query = string.Format ( "<Query>" +"<Where>"+ "<Eq>"+ "<FieldRef Name='Company_x0020_Code' />"+" <Value Type='Text'>"+ txt_Companycode.Text +"</Value>"+" </Eq>"+" </Where>"+" </Query>");
SPList list = webs.Lists["Companies"];
SPListItemCollection items = list.GetItems(query);
if (items.Count > 0)
{
txt_Companycode.Text = "";
Response.Redirect("Pages/ClaimAccount.aspx");
}
else
{
lbl_EmpCode.Text = "Company Code is not Matching with the Exisiting Record..!";
txt_Companycode.Text = "";
Response.Redirect("Pages/CompanyCodeCheck.aspx");
}
}
Following is my .ascx file
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Company_Code.ascx.cs" Inherits="Company_Code.Company_Code" %>
<table class="style1">
<tr>
<td class="style2">
<asp:Label ID="lbl_code" runat="server" Font-Bold="True" ForeColor="#0033CC"
Text="Enter Country Code"></asp:Label>
</td>
<td class="style3">
<asp:TextBox ID="txt_Companycode" runat="server" ></asp:TextBox>
</td>
<td>
<asp:Label ID="lbl_EmpCode" runat="server" ForeColor="#FF0066"></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
<asp:Button ID="btn_Submit" runat="server" Font-Bold="True" ForeColor="#3333FF"
Text="Submit" onclick="btn_Submit_Click" Width="67px" />
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please Enter Exisiting Company Code....!"
ControlToValidate="txt_Companycode"></asp:RequiredFieldValidator>
</td>
</tr>
</table>
What is wrong with this code? when i enter input in text box , it take me to error pape. I am comparing text box value with list Item , if it match then go to next page otherwise Error and remain in same page.
Here is the solution for this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.IO;
using System.Web.Configuration;
namespace UserControls
{
public partial class CheckCompanyCode : System.Web.UI.UserControl
{
public string siteUrl;
protected void Page_Load(object sender, EventArgs e)
{ }
protected void btn_submit_Click(object sender, EventArgs e)
{
string companyCode = txt_companyCode.Text.ToString().Trim();
try
{
SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();
SPContext currentContext = SPContext.Current;
SPList myList = myWeb.Lists["Companies"];
SPQuery myQuery = new SPQuery();
myQuery.Query = @"<Where><Eq><FieldRef Name = ''Company_x0020_Code''/><Value Type=''Text''>"
+ companyCode
+ "</Value></Eq></Where>";
SPListItemCollection items = myList.GetItems(myQuery);
if (items.Count == 1)
{
Response.Redirect("~/Pages/ClaimAccount.aspx?EmployerCode=" + companyCode);
}
else
{
lbl_EmpCode.Text = "Company Code doesn''t Match with the Exisiting Record..!";
}
}
catch (Exception ex)
{
Response.Write("<br><br>" + ex.Message.ToString());
}
}
}
}
这篇关于如何使用webCont用sp列表来组合txt框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!