TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Guest User
Tech Writer
271
36k
bind dropdown selected Id in asp.net
Aug 7 2020 5:01 AM
Hello..
Admin = 1
User= 2
EndUser= 3
i already give values to them and bind it.
now when someone is register usertypeid = selected value of dropdown..
here is my code..
<table
class
=
"style1"
>
<tr>
<td> Image:</td>
<td>
<asp:FileUpload ID=
"FileImg"
runat=
"server"
></asp:FileUpload>
</td>
</tr>
<tr>
<td> Name:</td>
<td>
<asp:TextBox ID=
"TxtName"
runat=
"server"
></asp:TextBox>
</td>
<asp:RequiredFieldValidator ID=
"RequiredFieldValidator1"
runat=
"server"
Style=
"top:182px; left: 774px; position: absolute; height: 26px; width: 162px"
ControlToValidate=
"TxtName"
ErrorMessage=
"Name required**"
font-size=
"Small"
ForeColor =
"Red"
></asp:RequiredFieldValidator>
</tr>
<tr>
<td>UserName:</td>
<td>
<asp:TextBox ID=
"TxtUserName"
runat=
"server"
></asp:TextBox>
</td>
<asp:RequiredFieldValidator ID=
"RequiredFieldValidator2"
runat=
"server"
Style=
"top: 224px; left: 774px; position: absolute; height: 26px; width: 162px"
ControlToValidate=
"TxtUserName"
ErrorMessage=
"Username required** "
font-size=
"Small"
ForeColor=
"Red"
></asp:RequiredFieldValidator>
</tr>
<tr>
<td>EmailID:</td>
<td>
<asp:TextBox ID=
"TxtEmailID"
runat=
"server"
></asp:TextBox>
</td>
<asp:RequiredFieldValidator ID=
"RequiredFieldValidator3"
runat=
"server"
Style=
"top:271px; left: 774px; position: absolute; height: 26px; width: 162px"
ControlToValidate=
"TxtEmailID"
ErrorMessage=
"Valid EmailID required"
font-size=
"Small"
ForeColor=
"Red"
></asp:RequiredFieldValidator>
</tr>
<tr>
<td>UserRole:</td>
<td>
<asp:dropdownlist runat=
"server"
id=
"ddlDropdown"
OnSelectedIndexChanged=
"ddlDropdown_SelectedIndexChanged"
>
</asp:dropdownlist></td>
</tr>
<tr>
<td>Password:</td>
<td>
<asp:TextBox ID=
"TxtPassword"
runat=
"server"
TextMode=
"Password"
></asp:TextBox>
</td>
<asp:RequiredFieldValidator ID=
"RequiredFieldValidator5"
runat=
"server"
TextMode=
"Password"
Style=
"top:321px; left: 774px; position: absolute; height: 26px; width: 162px"
ControlToValidate=
"TxtPassword"
ErrorMessage=
"It contains 1-special character and 0-9 numbers required"
font-size=
"Small"
ForeColor=
"Red"
></asp:RequiredFieldValidator>
</tr>
<tr>
<td>Confirm Password:</td>
<td>
<asp:TextBox ID=
"TxtConfirmPassword"
runat=
"server"
TextMode=
"Password"
></asp:TextBox> </td>
</tr>
<tr>
<td>
<asp:Label ID=
"LblMesge"
runat=
"server"
></asp:Label>
</td>
<td> </td>
</tr>
</table>
</div>
<asp:Button ID=
"Button1"
runat=
"server"
Text=
"Save"
class
=
"btn"
onclick=
"Button1_Click"
/>
protected
void
Page_Load(
object
sender, EventArgs e)
{
bindUsingEntityFrameWork();
}
public
void
bindUsingEntityFrameWork()
{
using
(WallpaperEntities5 context =
new
WallpaperEntities5())
{
ddlDropdown.DataSource = (from r
in
context.UserTypes select
new
{Role = r.Role, Role_Id = r.Role_Id }).ToList();
ddlDropdown.DataTextField =
"Role"
;
ddlDropdown.DataValueField =
"Role_Id"
;
ddlDropdown.DataBind();
ddlDropdown.Items.Insert(0,
new
System.Web.UI.WebControls.ListItem(
"Select Option"
,
"0"
));
}
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
Model.User newusermodel =
new
Model.User();
string
str = FileImg.FileName;
FileImg.PostedFile.SaveAs(Server.MapPath(
"~/Upload/"
+ str));
string
Image =
"~/Upload/"
+ str.ToString();
newusermodel.Image = Image;
newusermodel.Name = TxtName.Text;
newusermodel.UserName = TxtUserName.Text;
newusermodel.EmailID = TxtEmailID.Text;
newusermodel.Created_Date = DateTime.Now;
newusermodel.User_Type_Id =
newusermodel.Password = TxtPassword.Text;
using
(WallpaperEntities5 context =
new
WallpaperEntities5())
{
User user =
new
User();
{
string
name = FileImg.ToString();
user.Image = newusermodel.Image;
user.Name = newusermodel.Name;
user.UserName = newusermodel.UserName;
user.EmailID = newusermodel.EmailID;
user.Created_Date = newusermodel.Created_Date;
user.User_Type_Id = newusermodel.User_Type_Id;
user.Password = newusermodel.Password;
};
var usersadd = context.Set<User>();
var response = context.Users.Where(u => u.UserName == newusermodel.UserName).FirstOrDefault();
if
(response ==
null
)
{
usersadd.Add(user);
context.SaveChanges();
Response.Redirect(
"/Login.aspx"
);
}
}
}
Reply
Answers (
6
)
MVC Dropdownlist selected index changed event
How can we use face detection api in .net mvc?