我有一个Web表单asp.net,用于将特定信息保存到Visual Studio中的本地数据库中。这些Web表单组件中的一些是html组件。 点击"添加"按钮,这是作为asp.net按钮,数据成功保存到数据库中,但当我查看我的数据库表时,它显示我" System.Web.UI.HtmlControls.HtmlInputText"在某些领域,如下图所示:
让我感到困惑的是,我不知道为什么某些值会正确保存而其他值不能保存! eventName,EventDuration,EventAdmission,EventWebsite和EventVenue都是html文本框..并且如上图所示,对于所有这些字段,保存的值是" System.Web.UI.HtmlControls.HtmlInputText"而EventVenue值正确保存!!即使代码相同..请参阅下面的示例:
<div class="6u$ 12u$(small)">
<label for="website">Website</label>
<input type="text" id="website" name="website" runat="server" />
</div>
<div class="6u$ 12u$(small)">
<label for="venue">Venue</label>
<input type="text" id="venue" name="venue" runat="server" />
这是我的源代码:
protected void addbtn_Click(object sender, EventArgs e)
{
string latitude = "";
string longitude = "";
string imagesArr = null;
bool fields = false;
string eventname = Request.Form["name"].ToString();
string disc = Request.Form["TextArea3"].ToString();
string startDate = DropDownList1.Text + "/" + category.Value + "/" + DropDownList2.Text;
string endDate = DropDownList4.Text + "/" + days.Value + "/" + DropDownList5.Text;
string eventduration = Request.Form["duration"].ToString()
string eventadmission = admission.Value;
string categ = DropDownList3.Text;
string contact = Request.Form["TextArea4"].ToString();
string eventwebsite = website.Value;
string location = venue.Value;
country.Disabled = true;
string eventcountry = Request.Form["country"].ToString();
if (Session["Latitude"].ToString() == null && Session["Longitude"].ToString() == null)
{
latitude = "";
longitude = "";
}
else if (Session["Latitude"].ToString() != null && Session["Longitude"].ToString() != null)
{
latitude = Session["Latitude"].ToString();
longitude = Session["Longitude"].ToString();
}
if (Session["ImagesArray"].ToString() != null)
{
imagesArr = Session["ImagesArray"].ToString();
}
if (DropDownList2.Text.Equals("Year") || DropDownList1.Text.Equals("Month") || category.Value.Equals("Day") || DropDownList5.Text.Equals("Year") || DropDownList4.Text.Equals("Month") || days.Value.Equals("Day") || name == null || disc == null || duration == null || admission == null || categ == null || contact == null || website == null || location == null)
{
fields = true;
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert(' Make sure to enter all the required data')", true);
}
else { fields = false; }
if (fields == false)
{
command.CommandText = "INSERT INTO EventsEnglish(EventName,EventDescription,EventStartDate,EventEndDate,EventDuration, EventAdmission, EventCategory, EventContact, EventWebsite, EventVenue, EventMapLatitude, EventMapLongitude, CountryName, EventImages) values(N'" + name + "', N'" + disc + "',N'" + startDate + "',N'" + endDate + "',N'" + duration + "',N'" + admission + "', N'" + categ + "', N'" + contact + "', N'" + website + "', N'" + location + "', N'" + latitude + "', N'" + longitude + "', N'" + country + "', N'" + imagesArr + "')";
//command.ExecuteNonQuery();
int result = command.ExecuteNonQuery();
if (result == 1)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert(' New event has been added')", true);
name.Value = "";
TextArea3.InnerText = "";
DropDownList1.Text = "Month";
category.Value = "Day";
DropDownList2.Text = "Year";
DropDownList4.Text = "Month";
days.Value = "Day";
DropDownList5.Text = "Year";
duration.Value= "";
admission.Value = "";
DropDownList3.Text = "Category";
TextArea4.InnerText = "";
website.Value = "";
venue.Value = "";
Session.Remove("ImagesArray");
Session.Remove("Latitude");
Session.Remove("Longitude");
}
con.Close();
}
}
如上面的代码所示,我使用了两种不同的方法来获取html文本框值的值,例如&#34; Request.Form [&#34; name&#34;]。的ToString();&#34;和&#34; name.Value;&#34;并且没有一个工作,除了第二种方式适用于&#34; Venue&#34;只有字段。
你有什么想法解决这个问题吗?
答案 0 :(得分:0)
好吧,我在这段代码中找出了我的问题
command.CommandText =
"INSERT INTO EventsEnglish( " +
"EventName, EventDescription, EventStartDate, EventEndDate, " +
"EventDuration, EventAdmission, EventCategory, EventContact, " +
"EventWebsite, EventVenue, EventMapLatitude, EventMapLongitude, " +
"CountryName, EventImages) " +
"VALUES (" +
"N'" + name + "', N'" + disc + "', N'" + startDate + "', " +
"N'" + endDate + "',N'" + duration + "',N'" + admission + "', " +
"N'" + categ + "', N'" + contact + "', N'" + website + "', " +
"N'" + location + "', N'" + latitude + "', N'" + longitude + "', " +
"N'" + country + "', N'" + imagesArr + "')";
我只需要添加string local variables
而不是html text
框名称..我不知道我怎么没注意到这一点!!