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
Africa Treasury
NA
2
1.4k
loop through to find the control and apply to class
Mar 7 2018 4:16 AM
i need to fetch the record from database and in itemdatabound event loop through each item and find the control and apply the class that is in database.
If you run this code you will see that users can pick any icon of their choice on modal and it will appear on the datalist, the challenge am facing here is if i refresh the page the result displayed on datalist disappears, how can i fetch the record from database and in itemdatabound event loop through each item and find the control and apply the class that is in database.
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!
this
.IsPostBack)
{
this
.Populatebooks();
string
username5 =
this
.Page.User.Identity.Name;
//Path.GetFileName(Request.Url.AbsolutePath);
//}
}
}
private
void
Populatebooks()
{
string
Populatebooks =
"GetUserPOSTSEXAMPLE"
;
using
(SqlConnection con =
new
SqlConnection(constring4))
{
con.Open();
using
(SqlCommand cmd =
new
SqlCommand(Populatebooks, con))
{
cmd.CommandType = CommandType.StoredProcedure;
// cmd.Parameters.AddWithValue("@Name", Request.QueryString["Id"].ToString());
cmd.Parameters.AddWithValue(
"@UserName"
, Session[
"userName"
]);
// cmd.Parameters.AddWithValue("@Id", Id);
//lblpost.Text = TextBixcomment.Text.Replace(Environment.NewLine, "<BR />");
SqlDataAdapter sda =
new
SqlDataAdapter(cmd);
DataTable dt =
new
DataTable();
sda.Fill(dt);
ViewState[
"DataTable"
] = dt;
Getpost.DataSource = dt;
Getpost.DataBind();
}
}
}
[WebMethod]
public
static
string
INSERTUSERSTATUS(
string
userName,
string
icon,
string
postId)
{
string
constr = ConfigurationManager.ConnectionStrings[
"DB"
].ConnectionString;
using
(SqlConnection con =
new
SqlConnection(constr))
{
using
(SqlCommand cmd =
new
SqlCommand(
"INSERT INTO [UserPICKS](UserName,statuscount,Iconbook,IconCup,IconPen,PostId6) VALUES(@UserName,@statuscount,@Iconbook,@IconCup,@IconPen,@PostId6)"
))
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue(
"@UserName"
, userName);
cmd.Parameters.AddWithValue(
"@statuscount"
,
"1"
);
if
(icon.ToLower() ==
"fa fa-book"
)
{
cmd.Parameters.AddWithValue(
"@Iconbook"
,
"book"
);
cmd.Parameters.AddWithValue(
"@IconCup"
,
""
);
cmd.Parameters.AddWithValue(
"@IconPen"
,
""
);
}
if
(icon.ToLower() ==
"fa fa-coffee"
)
{
cmd.Parameters.AddWithValue(
"@Iconbook"
,
""
);
cmd.Parameters.AddWithValue(
"@IconCup"
,
"cup"
);
cmd.Parameters.AddWithValue(
"@IconPen"
,
""
);
}
if
(icon.ToLower() ==
"fa fa-pencil"
)
{
cmd.Parameters.AddWithValue(
"@Iconbook"
,
""
);
cmd.Parameters.AddWithValue(
"@IconCup"
,
""
);
cmd.Parameters.AddWithValue(
"@IconPen"
,
"pen"
);
}
cmd.Parameters.AddWithValue(
"@PostId6"
, postId);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
return
"posted"
;
}
<form id=
"form1"
runat=
"server"
>
<div
class
=
"wrapper"
>
</div>
<br /><br />
<div
class
=
""
>
<div
class
=
"container"
>
<asp:DataList ID=
"Getpost"
runat=
"server"
DataKeyName=
"Id"
Font-Bold=
"True"
Width=
"100%"
OnItemDataBound=
"Getpost_ItemDataBound"
>
<ItemTemplate>
<div
class
=
"well col-lg-6"
>
<asp:Label ID=
"Label1"
Text=
'<%# Eval("UserName") %>'
runat=
"server"
/>
<div
class
=
""
style=
"margin-top: 0; margin-left: 9px; margin-bottom: 8px"
>
<asp:Label ID=
"lblpost"
runat=
"server"
Text=
'<%# Eval("ContentPost").ToString().Length >180 ? Eval("ContentPost").ToString().Substring(0,180)+"...(Read More)" : Eval("ContentPost").ToString()%>'
Font-Bold=
"False"
Font-Strikeout=
"False"
ForeColor=
"#333333"
CssClass=
""
Font-Names=
"Arial"
Font-Size=
""
/>
</div>
<asp:Label ID=
"lbCOMlId"
Text=
'<%#Eval("ID") %>'
CssClass=
""
runat=
"server"
Font-Size=
"0px"
Visible=
"False"
/>
<asp:Label ID=
"lbCOMlId2"
Text=
'<%#Eval("ID") %>'
CssClass=
""
runat=
"server"
/>
<div
class
=
"clearfix"
>
</div>
<asp:LinkButton ID=
"btnPick"
Text=
""
runat=
"server"
>
<asp:Panel ID=
"Panel1"
runat=
"server"
>
<asp:Label ID=
"lblpickdefaultshow"
runat=
"server"
Text=
""
CssClass=
" fa fa-shopping-cart"
Font-Size=
"Larger"
></asp:Label>
<asp:Label ID=
"Label688"
runat=
"server"
Text=
""
Font-Size=
"Larger"
></asp:Label>
<asp:Label ID=
"Label277"
runat=
"server"
Text=
"Pick"
Font-Size=
"Larger"
></asp:Label>
</asp:Panel>
</asp:LinkButton>
</div>
</ItemTemplate>
</asp:DataList>
<asp:HiddenField ID=
"hfUserName"
runat=
"server"
/>
<asp:HiddenField ID=
"hfPostId"
runat=
"server"
/>
<br />
<div
class
=
"modal fade"
id=
"pickermodal"
tabindex=
"-1"
role=
"dialog"
aria-labelledby=
"pickerModalLabel"
aria-hidden=
"true"
>
<div
class
=
""
>
<div
class
=
"modal-dialog"
>
<div
class
=
"modal-content"
>
<div
class
=
"modal-header"
>
<button type=
"button"
class
=
"close"
data-dismiss=
"modal"
aria-label=
"Close"
>
<span aria-hidden=
"true"
>×</span></button>
<h4
class
=
"modal-title"
>
Pick Item Icons</h4>
</div>
<div
class
=
"modal-body"
>
....... <span style=
"margin-right: 29px"
>
<asp:LinkButton ID=
"btnpickbook"
Text=
""
runat=
"server"
>
<asp:Label ID=
"Label59"
runat=
"server"
Text=
""
Font-Size=
"42px"
CssClass=
"fa fa-book"
></asp:Label>
</asp:LinkButton>
<a href=
"#"
id=
""
></a></span><span style=
"margin-right: 29px"
><a href=
"#"
id=
"btnpickcup"
>
<asp:Label ID=
"Label7"
runat=
"server"
Text=
""
Font-Size=
"42px"
CssClass=
"fa fa-coffee"
ForeColor=
"Red"
></asp:Label>
</a></span><span style=
"margin-right: 29px"
><a href=
"#"
id=
"btnpen"
>
<asp:Label ID=
"Label8"
runat=
"server"
Text=
""
Font-Size=
"42px"
CssClass=
"fa fa-pencil "
></asp:Label>
</a></span>
</div>
<div
class
=
"modal-footer"
>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div></div>
<!-- /.modal -->
</div>
</div>
</form>
<script type=
"text/javascript"
src=
"http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"
></script>
<script src=
"../Script/default%20css/AdminLTE-2.2.0/bootstrapDefault/js/bootstrap.min.js"
></script>
<link rel=
"stylesheet"
href=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"
/>
<script type=
'text/javascript'
>
$(function () {
var lblPick;
var lbliconam;
$(
'[id*=btnPick]'
).on(
'click'
, function () {
lblPick = $(
this
).closest(
'tr'
).find(
'[id*=lblpickdefaultshow]'
);
lbliconam = $(
this
).closest(
'tr'
).find(
'[id*=Label277]'
);
var username = $(
this
).closest(
'tr'
).find(
'[id*=Label1]'
).text();
var postId = $(
this
).closest(
'tr'
).find(
'[id*=lbCOMlId2]'
).text();
//var postId = "106";
$(
'[id*=hfUserName]'
).val(username);
$(
'[id*=hfPostId]'
).val(postId);
$(
'[id*=pickermodal]'
).modal(
'show'
);
return
false
;
});
$(
'[id*=pickermodal] a'
).click(function () {
var icon =
''
;
var iconName =
''
;
var userName = $(
'[id*=hfUserName]'
).val();
var postId = $(
'[id*=hfPostId]'
).val();
if
($(
this
).find(
'[id*=Label59]'
).length > 0) {
icon =
'fa fa-book'
;
iconName =
'Book'
;
}
else
if
($(
this
).find(
'[id*=Label7]'
).length > 0) {
icon =
'fa fa-coffee'
;
iconName =
'Cup'
;
}
else
if
($(
this
).find(
'[id*=Label8]'
).length > 0) {
icon =
'fa fa-pencil'
;
iconName =
'Pen'
;
}
$.ajax({
type:
"POST"
,
url:
"CS.aspx/INSERTUSERSTATUS"
,
data:
'{icon:"'
+ icon +
'",iconName:"'
+ iconName +
'",userName:"'
+ userName +
'",postId:"'
+ postId +
'"}'
,
contentType:
"application/json; charset=utf-8"
,
dataType:
"json"
,
success: function (response) {
$(
'#pickermodal'
).modal(
'hide'
);
$(
'[id*=hfUserName]'
).val(
''
);
$(
'[id*=hfPostId]'
).val(
''
);
$(lblPick).attr(
'class'
, icon);
$(lbliconam).html(iconName);
},
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
return
false
;
});
});
</script>
protected
void
Getpost_ItemDataBound(
object
sender, DataListItemEventArgs e)
{
//display the result
Reply
Answers (
1
)
Data Access In Models & Repositories
Open documents in new tab