我有一个Web窗体,asp.net,将特定的信息保存到Visual Studio中的本地数据库中。这些Web表单组件中的一部分是html组件。 点击一个“添加”按钮,这是作为asp.net按钮,数据被成功保存到数据库中,但是当我查看我的数据库表时,它显示我“System.Web.UI.HtmlControls.HtmlInputText”中某些字段如下图所示:保存System.Web.UI.HtmlControls.HtmlInputText而不是文本框的值asp.net
什么让我困惑的是,我不知道为什么有些值是正确保存和别人不! 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文本框值的值,例如“Request.Form [”name“]。ToString();”和“name.Value;”除了第二种方法只适用于“Venue”字段外,他们都没有工作。
你有什么想法解决这个问题?
尝试从html代码中移除'runat =“server”'并使用'value'属性获取文本。 'Request.Form [“”]'应该从DOM文本框中读取值。 –
如果我删除了runat =“server”,那么源代码无法识别html文本框,它看不到它。 – user3599431