I have a repeater on my web page that displays Tests data
<form id="frmQuestion">
<div class="panel panel-primary">
<div class="panel-body" style="max-height: 420;overflow-y: scroll;">
<asp:ScriptManager ID="scriptmgr" runat="server"> </asp:ScriptManager>
<asp:UpdatePanel ID="upd" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater ID="rptQuestions" runat="server">
<HeaderTemplate>
<table class="table" cellpadding="5px" cellspacing="10px" border="solid" id="tableQuestion">
<tr style="color:Black; background-color:Teal; border:solid;">
<th style="width:20px">No.</th>
<th style="width:100px">Question</th>
<th style="width:40px">QuestionType</th>
<th style="width:110px">Text Answer</th>
<th style="width:100px">Multiple Choice 1</th>
<th style="width:100px">Multiple Choice 2</th>
<th style="width:100px">Multiple Choice 3</th>
<th style="width:100px">Multiple Choice 4</th>
<th style="width:70px">True/False</th>
<th>Controls</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="color:Black; background-color:Silver; border:solid;" id="trdata" class="trclass">
<td id="no"><asp:Label runat="server" ID="lblNo" Text='<%#Eval("Qno") %>' /></td>
<td id="que"><asp:Label runat="server" ID="lblQuestion" Text='<%#Eval("Question") %>'></asp:Label>
<asp:TextBox runat="server" ID="txtQuestion" Text='<%#Eval("Question") %>' Visible="false"></asp:TextBox>
<asp:Label runat="server" ID="lblQid" Text='<%#Eval("Qid") %>' Visible="false"></asp:Label></td>
<td><asp:Label runat="server" ID="lblQtype" Text='<%#Eval("Qtype") %>' ></asp:Label>
<asp:DropDownList runat="server" ID="ddlQType" Visible="false"></asp:DropDownList>
<%-- <select class="dropdown" id="ddlQType" name="ddlQType" style="width:100px" runat="server">
<option value="0">--Select--</option>
<option value="1">Descriptive Answer</option>
<option value="2">Multiple Choice </option>
<option value="3">True/False answer</option></select>--%></td>
<td><asp:Label runat="server" ID="lblAnsDesc" Text='<%#Eval("description") %>' Width="100px" ></asp:Label>
<asp:TextBox runat="server" ID="txtAnsDesc" Text='<%#Eval("description") %>' Width="100px" Visible="false"></asp:TextBox></td>
<td><asp:Label runat="server" ID="lblch1" Text='<%#Eval("choice1") %>' Width="100px" ></asp:Label>
<asp:TextBox runat="server" ID="txtch1" Text='<%#Eval("choice1") %>' Width="100px" Visible="false"></asp:TextBox></td>
<td><asp:Label runat="server" ID="lblch2" Text='<%#Eval("choice2") %>' Width="100px" ></asp:Label>
<asp:TextBox runat="server" ID="txtch2" Text='<%#Eval("choice2") %>' Width="100px" Visible="false"></asp:TextBox></td>
<td><asp:Label runat="server" ID="lblch3" Text='<%#Eval("choice3") %>' Width="100px" ></asp:Label>
<asp:TextBox runat="server" ID="txtch3" Text='<%#Eval("choice3") %>' Width="100px" Visible="false"></asp:TextBox></td>
<td><asp:Label runat="server" ID="lblch4" Text='<%#Eval("choice4") %>' Width="100px" ></asp:Label>
<asp:TextBox runat="server" ID="txtch4" Text='<%#Eval("choice4") %>' Width="100px" Visible="false"></asp:TextBox>
<asp:Label runat="server" ID="lblCorrect" Text='<%#Eval("correct") %>' /></td>
<td><asp:Label runat="server" ID="lblisTrue" Text='<%# Eval("isTrue") %>' Width="70px" ></asp:Label>
<asp:CheckBox runat="server" ID="chkistrue" Checked='<%#(Eval("isTrue")=="true") %>' Width="70px" Visible="false"/></td>
<td> <span class="more"><asp:LinkButton ID="lnkEdit" Text="Edit" runat="server" data-toggle="modal" data-target="#myModal" OnClientClick="getdata();" CausesValidation="false" />
<asp:LinkButton ID="lnkUpdate" Text="Update" runat="server" Visible="false" OnClick="OnUpdate" CausesValidation="false" />
<asp:LinkButton ID="lnkCancel" Text="Cancel" runat="server" Visible="false" OnClick="OnCancel" CausesValidation="false" />
<asp:LinkButton ID="lnkDelete" Text="Delete" runat="server" OnClick="OnDelete" CausesValidation="false" OnClientClick="return confirm('Do you want to delete this row?');" />
</span></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
<!-- Modal example -->
Add Question    
<!-- Modal -->
×
RESULT
--Select--
Descriptive Answer
Multiple Choice
True/False answer
Close
</form>
Now I want to get repeater row data ie question details on btnUpdate_Click event. The server code is
protected void BindRepeater(int tid)
{
try
{
cmd = new SqlCommand("SP_GetQuestions", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#Tid", tid);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
connection.Close();
if (dt.Rows.Count > 0)
{
//adding qno to dt
dt.Columns.Add("Qno",typeof(System.Int32));
for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[i]["Qno"] = i + 1;
}
rptQuestions.DataSource = dt;
rptQuestions.DataBind();
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected Question getData()
{
Question question = new Question();
question.qtype = Convert.ToInt32(lblQuestionType.Text);
switch (question.qtype)
{
case 1:
question.ansdesc = textareaAns.InnerText.ToString();
break;
case 2:
question.ch1 = txtMulti1.Text;
question.ch2 = txtMulti2.Text;
question.ch3 = txtMulti3.Text;
question.ch4 = txtMulti4.Text;
question.correct = "";
if (chkmulti1.Checked == true)
question.correct += "1";
if (chkmulti2.Checked == true)
question.correct += "2";
if (chkmulti3.Checked == true)
question.correct += "3";
if (chkmulti4.Checked == true)
question.correct += "4";
break;
case 3:
if (rbtrue.Checked == true)
question.isTrue = true;
if (rbfalse.Checked == true)
question.isTrue = false;
break;
}
return question;
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
Question question = getData();
question.qid = Convert.ToInt32(lblQuestid.Text);
cmd = new SqlCommand("SP_UpdateQuestion", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#question", question.questDesc);
cmd.Parameters.AddWithValue("#qid", question.qid);
cmd.Parameters.AddWithValue("#ans", question.ansdesc);
cmd.Parameters.AddWithValue("#ch1", question.ch1);
cmd.Parameters.AddWithValue("#ch2", question.ch2);
cmd.Parameters.AddWithValue("#ch3", question.ch3);
cmd.Parameters.AddWithValue("#ch4", question.ch4);
cmd.Parameters.AddWithValue("#correct", question.correct);
cmd.Parameters.AddWithValue("#isTrue", question.isTrue);
cmd.Parameters.AddWithValue("#qtype", question.qtype);
try
{
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
Response.Write("<script>alert('Question updated successfully!');</script>");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
But compiler error occurs as ASP.addquestion_aspx' does not contain a definition for 'OnUpdate' and no extension method 'OnUpdate' accepting a first argument of type 'ASP.addquestion_aspx'
I think button is outside repeater hence the error. How can I access repeater's data in order to update it. Please guide me.
Actually the error was caused because I did not define OnUpdate() method for lnkUpdate button inside repeater. I just removed that button as I no longer needed it and it is working fine now.
Related
I'm trying to retain the index of the Datalist item on page reload. I'm trying to do this by storing the index into a Session and then setting the index of DataList object equals to the Session. Here's the HTML code:
<asp:DataList ID="MyList" Width="100%" RepeatDirection="Vertical" runat="server"
OnItemDataBound="MyList_ItemDataBound" OnItemCommand="MyList_ItemCommand">
<ItemTemplate>
<table class="table">
<tr>
<td style="width: 5%;">
<asp:Label ID="lbl_id" runat="server" Text='<%# Eval("id") %>' Visible="false" />
<asp:Label ID="lbl_isread" runat="server" Text='<%# Eval("isread") %>' Visible="false" />
<input type="checkbox" id="chk_vd" runat="server" class="chkitem" />
</td>
<td style="width: 15%;">
<asp:HyperLink ID="usr" runat="server" Text='<%# Eval("from") %>' />
</td>
<td style="width: 62%;">
<asp:LinkButton ID="sub" Text='<%# Eval("subject") %>' CommandName="select" runat="server"></asp:LinkButton>
</td>
<td style="width: 18%;">
<asp:Label ID="dt" runat="server" Text='<%# Eval("Date_Added") %>' />
</td>
</tr>
</table>
</ItemTemplate>
<SelectedItemTemplate>
<table class="table">
<tr>
<td style="width: 5%;">
<asp:Label ID="lbl_id" runat="server" Text='<%# Eval("id") %>' Visible="false" />
<asp:Label ID="lbl_isread" runat="server" Text='<%# Eval("isread") %>' Visible="false" />
<asp:Label ID="lbl_messagetype" runat="server" Text='<%# Eval("messagetype") %>'
Visible="false" />
<asp:Label ID="lbl_groupid" runat="server" Text='<%# Eval("groupid") %>' Visible="false" />
<asp:Label ID="lbl_content_id" runat="server" Text='<%# Eval("content_id") %>' Visible="false" />
<asp:Label ID="lbl_sendertype" runat="server" Text='<%# Eval("sendertype") %>' Visible="false" />
<input type="checkbox" id="chk_vd" runat="server" class="chkitem" />
</td>
<td style="padding: 5px; text-align: left; vertical-align: top; width: 15%;">
<uc1:avator ID="avt" runat="server" UserName='<%# Eval("from") %>' PhotoName="" Width="65"
Height="65" />
<br />
<asp:HyperLink ID="usr" runat="server" Text='<%# Eval("from") %>' />
</td>
<td style="padding: 5px; text-align: left; vertical-align: top; width: 62%;">
<div class="item_pad_4">
<h3 id="s_sub" runat="server">
<%# Eval("subject") %>
</h3>
</div>
<asp:Label ID="msg" runat="server" Text='<%# Eval("body") %>'></asp:Label>
<div class="bx_br_tp item_pad_4">
<div class="btn-group" id="<%# Eval("id") %>">
<asp:LinkButton ID="a" CssClass="btn btn-primary btn-xs" runat="server" />
<asp:LinkButton ID="r" CssClass="btn btn-primary btn-xs" runat="server" />
<%-- <asp:LinkButton ID="lnk_spam" CssClass="btn btn-primary btn-xs" runat="server"
Text="spam" CommandName="spam"></asp:LinkButton>--%>
</div>
</div>
</td>
<td style="padding: 5px; text-align: left; vertical-align: top; width: 18%;">
<asp:Label ID="dt" runat="server" Text='<%# Eval("Date_Added") %>' />
</td>
</tr>
</table>
</SelectedItemTemplate>
</asp:DataList>
On the server side, I'm trying to restore the index of the DataList item as follows:
if (!Page.IsPostBack)
{
if (Session["datalist"] != null)
{
((DataList)Session["datalist"]).SelectedIndex = (int)Session["index"];
}
Here's the function which does data binding :
protected void MyList_ItemCommand(object source, DataListCommandEventArgs e)
{
Session["datalist"] = source;
string cmd = ((LinkButton)e.CommandSource).CommandName;
long messageid = long.Parse(((Label)e.Item.FindControl("lbl_id")).Text);
string username = ((HyperLink)e.Item.FindControl("usr")).Text;
// approve / ignore action related values
long content_id = 0;
long groupid = 0;
int messagetype = 0;
int index = e.Item.ItemIndex;
Session["index"] = index;
index = (int)Session["index"];
switch (cmd)
{
case "select":
((DataList)source).SelectedIndex = e.Item.ItemIndex;
int isread = int.Parse(((Label)e.Item.FindControl("lbl_isread")).Text);
// mark message as read it its unread message
if (isread == 0)
{
isread = 1; // message is read
MailBoxBLL.Update_isRead(messageid, isread, Page.User.Identity.Name);
}
string _cache = "usr_msg_cnt_" + UserName;
if (HttpContext.Current.Cache[_cache] != null)
{
Cache.Remove(_cache);
}
break;
This doesn't work and Datalist item does not get selected when the page reloads. How can I make it work?
Any help is appreciated.
Add this directly in Page Load:-
protected void Page_Load(object sender, EventArgs e)
{
if (Session["datalist"] != null)
((DataList)Session["datalist"]).SelectedIndex = (int)Session["index"];
}
The probelm with your code is that the Page.IsPostBack property will be false in the initial get request and for every page reload (actual postback) it will be true. Thus !Page.IsPostBack will become actually false and your code will never execute.
I have this piece of code which works fine and highlights the label. However, I want it to highlight the entire cell not just the label.
Any help would be appreciated!
protected void HighLight_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Label TotalTimeLabel = (Label)e.Item.FindControl("TotalTime");
if (TotalTimeLabel != null)
{
Decimal Total;
if (Decimal.TryParse(TotalTimeLabel.Text, out Total) == true)
{
if (Total > (Decimal)1.5)
{
TotalTimeLabel.BackColor = System.Drawing.Color.Red;
TotalTimeLabel.ForeColor = System.Drawing.Color.Black;
}
}
}
}
}
The code for the table is below
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="HighLight_ItemDataBound" >
<LayoutTemplate>
<table cellpadding="1" class="TableCSS" runat="server" id="tblProducts">
<tr runat="server" style="background-color:lightgrey">
<th runat="server">enNotificationNoNI</th>
<th runat="server">TotalTime</th>
<th runat="server">TPTIMEIN</th>
<th runat="server">Status</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server" class="TableData">
<td>
<asp:Label ID="enNotificationNoNI" runat="Server" Text='<%#Eval("enNotificationNoNI") %>' />
</td>
<td>
<asp:Label ID="TotalTime" runat="Server" Text='<%#Eval("TotalTime") %>' />
</td>
<td>
<asp:Label ID="TPTIMEIN" runat="Server" Text='<%#Eval("TPTIMEIN") %>' />
</td>
<td>
<asp:Label ID="Status" runat="Server" Text='<%#Eval("Status") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Try this code
For entire row
Control ctrl = TotalTimeLabel.Parent.Parent;
HtmlTableRow tblrw = (HtmlTableRow)ctrl;
tblrw.BgColor = System.Drawing.Color.Red.Name;
For one cell
Control ctrl = TotalTimeLabel.Parent;
HtmlTableCell tblcl = (HtmlTableCell)ctrl;
tblcl.BgColor = System.Drawing.Color.Red.Name;
let me know whether this works :)
I am currently building a CRUD operations on a entity called Payment.
When are click the pages, for instance, page 2, and try to delete the 9th item on page 2,
it will delete the 9th item on page 1 for me.
Here is my code:
protected void btnDelete_Click(object sender, CommandEventArgs e)
{
int paymentId = Convert.ToInt32(e.CommandArgument.ToString());
try
{
using (BillingApplicationDataContext dc = new BillingApplicationDataContext())
{
BLLPayment bllCorporation = new BLLPayment(dc);
bllCorporation.DeletePayment(paymentId);
}
Response.RedirectToRoute("ViewPaymentsRoute");
}
catch
{
Response.RedirectToRoute("ErrorPageRoute", new {
ErrorMsg = "Unable to delete Payment with Id " +
paymentId.ToString() + "." });
}
}
<asp:ListView ID="lstPayment" runat="server" OnPagePropertiesChanging="lstPayment_PagePropertiesChanging"
EnableViewState="false">
<LayoutTemplate>
<fieldset>
<legend>View Payments</legend>
<table cellpadding="0" cellspacing="0">
<tr>
<th>Id</th>
<th>Corporation</th>
<th>Service Contract</th>
<th>Payment Date</th>
<th>Payment Amount</th>
</tr>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</table>
</fieldset>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Id")%>' EnableViewState="false"/></td>
<td><asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CorpName")%>' EnableViewState="false"/></td>
<td><asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ServiceContractName")%>' EnableViewState="false"/></td>
<td><asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PaymentDate")%>' EnableViewState="false" /></td>
<td><asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Amount")%>' EnableViewState="false" /></td>
<td>
<%--<asp:Button runat="server" Text="Detail"
OnCommand="btnDetail_Click"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Id")%>' EnableViewState="true"/>--%>
<asp:Button ID="btnDelete" Text="Delete" runat="server"
oncommand='btnDelete_Click' CommandArgument='<%# Eval("Id") %>'
OnClientClick="return ConfirmDelete();"/></td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:DataPager ID="dataPager" runat="server" PagedControlID="lstPayment" EnableViewState="true">
<Fields>
<asp:NumericPagerField ButtonType="Link" />
</Fields>
</asp:DataPager>
<asp:Button runat="server"
ID="btnNew"
oncommand='btnNew_Click'
Text="New"/>
I think there may be something wrong with the page life cycle, but I do not know how to trace.
Why have you commented out
<asp:Button runat="server" Text="Detail"
OnCommand="btnDetail_Click"
**CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Id")%>'** EnableViewState="true"/>
You are trying to retreive the paymentId but you are passing Id as the command argument. I think the line above is absolutely correct.
I have nested Repeater. i am using 3 repeater. For calling(firing) the 1st repeater which is working fine and returning 100% result. But when i am calling(firing) 2nd repeater. it is not firing. I just want to know should i call 2nd repeater event if it is in nested format? Or i can not call. If i can call can u suggest how can i call that event?
<asp:UpdatePanel ID="RepeaterPanel" runat="server">
<ContentTemplate>
<div id="mydiv">
<asp:Repeater ID="RpterShareDetails" runat="server" onitemcommand="RpterShareDetails_ItemCommand">
<HeaderTemplate>
<table id="table1">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td rowspan="2" align="center" class="message_repeater_panel_left">
<asp:Image ID="Image1" runat="server" ImageUrl="~/ShowImage.ashx" alt="" width="50" height="50" />
</td>
<td>
<embed src='<%# Eval("FilePath") %>' type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="150" height="150" runat="server" Visible='<%# IsVisible(Eval("UploadType"))%>'></embed>
<asp:ImageButton ID="ibtnHolder" runat="server" Width="130" Height="130" ImageUrl='<%# Eval("FilePath") %>' Visible='<%# NotVisible(Eval("UploadType"))%>' />
</td>
<td class="textTob">
<asp:Label ID="UserCommentLabel" runat="server" Text='<%# Eval("Comments") %>' />
</td>
</tr>
<tr>
<td valign="bottom"> User Keyword:
<asp:Label ID="UserKeywordLabel" runat="server" Text='<%# Eval("Keyword") %>' />
</td>
</tr>
<tr>
<td colspan="2">
<div class="message_repeater_links12">
<span>
<asp:LinkButton ID = "lnkLike" runat = "server" CommandName = "Like">Like</asp:LinkButton>
</span>
<span>
<asp:LinkButton ID="lnk_Comments" runat="server" CommandName="comments" CommandArgument='<%# Eval("Sniffid") %>'>Comments</asp:LinkButton></span>
<span>
<asp:LinkButton ID="lnk_ShowComments" runat="server" CommandName="Show" CommandArgument='<%# Eval("Sniffid") %>'>Show</asp:LinkButton></span>
<span>Share</span>
<asp:Panel ID="Add_Comments" runat="server" Width="90%" class="message_repeater_panel_left">
</asp:Panel>
<asp:Panel ID="Dis_comments" runat="server" Width="90%" class="message_repeater_panel_left">
</asp:Panel>
</div>
</td>
</tr>
<asp:TextBox ID="txt_comment" runat="server" TextMode="MultiLine" Width="300" Visible="false"></asp:TextBox>
<asp:UpdatePanel ID="RepeaterPanel" runat="server" >
<ContentTemplate>
<div style="background-color: #C0C0C0">
<asp:Repeater ID="Rbt_comments" runat="server" onitemcommand="Rbt_comments_ItemCommand1" >
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<%--<div class="message_repeater_panel_left1" >--%>
<tr>
<td>
<span >
<img src="images/photo_sniff.jpg" width="25px" />
<%--<asp:Image ID = "imgCommentPhotos" runat = "server" ImageUrl = "~/AddCommentPhotos.ashx" Width = "25" Height = "25" />--%>
</span>
</td>
<td>
<span><asp:Label ID="UserCommentLabel" runat="server" Text='<%# Eval("Comments") %>' /></span>
</td>
</tr>
<%--</div>
<div class="message_repeater_links12"> --%>
<tr>
<td>
<span><asp:LinkButton ID = "lnkLike" runat = "server" CommandName = "SubLike">Like</asp:LinkButton></span>
</td>
<td>
<span><asp:LinkButton ID="lnk_Comments" runat="server" CommandName="Subcomments">Comments</asp:LinkButton></span>
</td>
<td>
<span><asp:LinkButton ID="lnk_ShowComments" runat="server" CommandName="SubShow">Show</asp:LinkButton></span>
</td>
<td>
<span>Share</span>
</td>
</tr>
<%--</div>--%>
<tr>
<td colspan="4">
<asp:TextBox ID = "txtComment" runat = "server" TextMode="MultiLine" Width="300" Visible = "false"></asp:TextBox>
<div class="message_repeater_panel_left1" ></div>
<div class="message_repeater_links12"></div>
</td>
</tr>
<ContentTemplate>
<div style="background-color: #C0C0C0">
<asp:Repeater ID="rptSubComments" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<span >
<img src="images/Santosh.jpg" width="25px" />
<%--<asp:Image ID = "imgCommentPhotos" runat = "server" ImageUrl = "~/AddCommentPhotos.ashx" Width = "25" Height = "25" />--%>
</span>
</td>
<td>
<span><asp:Label ID="lblUserSubComment" runat="server" Text='<%# Eval("Comments") %>' /></span>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</div>
</ContentTemplate>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
<SeparatorTemplate>
<br />
</SeparatorTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
<br />
<br />
<uc1:CustomPagination ID="RepeaterCustomPagination" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
protected void Rbt_comments_ItemCommand1(object source, RepeaterCommandEventArgs e)
{
try
{
if (e.CommandName == "Subcomments")
{
}
}
catch (Exception ex)
{
}
}
protected void RpterShareDetails_ItemCommand(object source, RepeaterCommandEventArgs e)
{
try
{
if (e.CommandName == "Like")
{
Int32 UserId = Convert.ToInt32(Session["UserID"].ToString());
LikeShareDetails(UserId);
}
if (e.CommandName == "comments")
{
LinkButton btn = e.CommandSource as LinkButton;
if (btn != null)
{
TextBox txt = (TextBox)e.Item.FindControl("txt_comment");
txt.Visible = true;
txt.Attributes.Add("OnKeyPress", "isNumberKey(this,"+ e.CommandArgument.ToString() +")");
Panel Panel2 = (Panel)e.Item.FindControl("Add_Comments");
Literal lt = new Literal(); lt.Text = "<br />";
Panel2.Controls.Add(lt);
Panel2.Controls.Add(txt);
DataTable dt = new DataTable();
Repeater gv = (Repeater)e.Item.FindControl("Rbt_comments");
SqlConnection sqlCon = new SqlConnection();
sqlCon.ConnectionString = ConfigurationManager.ConnectionStrings["ISnifferDBConnectionString"].ConnectionString;
//sqlCon.ConnectionString = CommonUtil.GetConfigValue("ISnifferDBConnectionString");
string strSql = "select Comments from Comments where SniffId in ('" + e.CommandArgument.ToString() + "') order by CommentId desc";
SqlCommand sqlComd = new SqlCommand(strSql, sqlCon);
sqlCon.Open();
SqlDataAdapter sqlAdpt = new SqlDataAdapter();
sqlAdpt.SelectCommand = sqlComd;
sqlAdpt.Fill(dt);
int row = dt.Rows.Count;
if (row >= 1)
{
gv.DataSource = dt;
gv.DataBind();
}
else
{
dt.Rows.Add("No Comments");
gv.DataSource = dt;
gv.DataBind();
}
Panel Panel1 = (Panel)e.Item.FindControl("Dis_comments");
Panel1.Controls.Add(gv);
}
}
}
}
In Nridubai website,i am using listview EditTemplate for editing purpose. In my EditTemplate, there are controls like..
<asp:TextBox ID="txtEditEventName" runat="server"
Text='<%# Bind("event_name") %>' />
And a few more controls like dropdownlist, calender controls. Now I want to validate using javascript on these controls, but its not working.
Eg.
var eventStatus=document.getElementById("<%=txtEditEventName.ClientID%>").value;
I am not using validation controls. Please help me how to use javascript for validation on EditTemplate Controls? My EditTemplate structure is like the following:
<EditItemTemplate>
<td class="command"><asp:LinkButton ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
<asp:LinkButton ID="LinkButton2" runat="server" Text="Update" CommandName="Update" />
</td>
<div class="header">View Details for '<%# Eval("event_name")%>'</div>
<tr>
<td class="edit" colspan="6" >
<div class="details">
<table class="detailview" cellpadding="0" cellspacing="0">
<tr>
<td>Event Name:</td>
<td>
<asp:TextBox ID="txtEditEventName" runat="server"
Text='<%# Bind("event_name") %>' />
</td>
<td>VenueAddress :</td>
<td>
<asp:TextBox ID="txtEditVenue" runat="server" Text='<%# Bind("venue") %>' />
</td>
</tr>
<tr>
<td>Country :</td>
<td>
<asp:DropDownList ID="lstEditCountry" runat="server"
Width="174" />
</td>
<td>Event Status:</td>
<td>
<asp:DropDownList ID="lstEditStatus" runat="server" Width="175px" >
<asp:ListItem value='0' Selected="True">-Select-</asp:ListItem>
<asp:ListItem >In-Progress</asp:ListItem>
<asp:ListItem >Completed</asp:ListItem>
<asp:ListItem >Aborted</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>Category :</td>
<td>
<asp:DropDownList ID="lstEditCategory" runat="server"
Width="174" />
</td>
</tr>
<tr>
<td>Start Date:</td>
<td>
<asp:TextBox ID="txtEditStartDate" runat="server"
Text='<%# Bind("start_date", "{0:dd/MM/yyyy}") %>' />
</td>
<td>End Date:</td>
<td>
<asp:TextBox ID="txtEditEndDate" runat="server"
Text='<%# Bind("end_date","{0:dd/MM/yyyy}") %>' />
</td>
</tr>
</table>
<div class="footer command">
<asp:LinkButton ID="LinkButton1" runat="server" Text="Close" CommandName="Cancel" />
</div>
</div>
</td>
</tr>
</EditItemTemplate>
You can access the elements on ItemDataBound and emit their ClientIDs for your JavaScript to use:
ItemDataBound:
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
StringBuilder vars= new StringBuilder();
if (ListView1.EditItem != null)
{
TextBox txtEditStartDate = ListView1.EditItem.FindControl("txtEditStartDate") as TextBox;
TextBox txtEditEndDate = ListView1.EditItem.FindControl("txtEditEndDate") as TextBox;
//example js, however I recommend passing the ClientIDs to functions
vars.Append(String.Format("var txtEditStartDate='{0}';" txtEditStartDate.ClientID);
vars.Append(String.Format("var txtEditStartDate='{0}';", txtEditEndDate.ClientID );
ClientScriptManager.RegisterStartUpScript(this.GetType(), "validationVars", vars.ToString(), true);
}
}
***Old Answer, the .NET way************
EditTemplate:
<asp:TextBox ID="txtEditEventName" runat="server"
Text='<%# Bind("event_name") %>' />
<asp:RequiredFieldValidator
id="rfvEditEventName"
ClientValidationFunction="txtEditEventNameClientValidate"
ControlToValidate="txtTitle"
runat="server"
Display="dynamic">*
</asp:RequiredFieldValidator>
JS:
function txtEditEventNameClientValidate(sender, args) {
if (args.Value == '') {
args.IsValid = false; // field is empty
//so something
}
else {
//do something
}
}
Put the validation javascript in the EditTemplate itself. This way, when it switches to edit-mode, the control will be in the context.