how to use if condition in aspx page - c#

I had placed div with condition.. on my execution,if condition is also displayed, i need to remove it..
**My New Code**
<asp:TemplateField HeaderStyle-Width="90px" ItemStyle-Width="0">
<ItemTemplate>
<div style="cursor: pointer; padding-top: 02px;" onclick="ShowfllDetails(<%#Eval("StudentID")%>);">
if(<%# (Eval("StatusName").Equals("Processed")) %>)
{
//should not show the upload button
}
else
{
<u>Upload </u> //show the upload button
}
</div>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#(Eval("StatusName").Equals("Processed") ? "images/add_btn.png" : "")%>' />
</ItemTemplate>
I'm getting displayed the if condition i need not to display it..
Thank-You.

I have done such a functionality inside Gridview, I assume you are doing the same. In place of tag you can use linkbutton set commandArgument and Commandname properties. AFter that fire Gridview_Rowcommand event. when ever you will click on linkbutton this event will fire and you can set the status in database or somewhere in session that this link is clicked against student id
<asp:LinkButton ID="LinkButton1" runat="server" Text="Upload" CommandName="Upload"
CommandArgument='<%#Eval("StudentID")%>'></asp:LinkButton>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Upload")
{
// get student id against clicked link button
int studentid = Convert.ToInt16(e.CommandArgument);
// -- set status in database it is clicked
}
}
after this bind your grid and on rowdatabound find control and set the visibility to true or false (Processed/Not Processed)
Bind your "StatusName" database field to a label and set the visibility to false of Label so that it should not display.
Now take idea from following code
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Display the company name in italics.
Label lblAssignto = (Label)e.Row.FindControl("lblassignto");
LinkButton addevent = (LinkButton)e.Row.FindControl("lnkBtnAddEvent");
LinkButton showevent = (LinkButton)e.Row.FindControl("lnkBtnShowEvent");
if (string.IsNullOrEmpty(lblAssignto.Text))
{
addevent.Visible = false;
showevent.Visible = false;
}
else
{
addevent.Visible = true;
showevent.Visible = true;
}
}
}

Related

button click event in repeater control to show data in same repeater in asp.net c#

I have a Button inside a repeater on whose click textbox should be visible to user to enter, but i have a list of button and clicking on specific button textbox should open specifically for that button only,
Currently when i click button all the textboxs gets visible to user.
Here Is The Code....
<asp:Repeater ID="rpt">
<div align="right" id="reply">
<asp:LinkButton ID="lnkbtnreply" OnClick="lnkbtnreply_Click" Text="Reply"></asp:LinkButton>
</div>
<asp:TextBox ID="" placeholder="Enter Your Reply Here" Visible="false">
</asp:TextBox>
</asp:Repeater>
Code Behind:
protected void lnkbtnreply_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in rptcomment.Items)
{
Panel replypic = (Panel)item.FindControl("replypic");
Panel replywrite = (Panel)item.FindControl("replywrite");
replypic.Visible = true; replywrite.Visible = true;
}
}
I have Found The Answer. In Case You Guys Are Still Searching For It Have A Look :
Here is the Code Behind:
protected void rptcomment_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Panel replypic = (Panel)e.Item.FindControl("replypic");
Panel replywrite = (Panel)e.Item.FindControl("replywrite");
if (e.CommandName == "img_Click") // check command is cmd_delete
{
// get you required value
string CustomerID = (e.CommandArgument).ToString();
replypic.Visible = true;
replywrite.Visible = true;
}
}
}

Check if hyperlink in gridview has been clicked c#

Hi I would like to know how to get the text field and index from a hyperlink in a gridview that has been clicked. Basically, the user would click on the hyperlink in the gridview and when the user has been navigated to the link, the text field and index of the link would be stored into an arraylist. Does anyone have any idea how I can go about doing this?
I have came up with this "pseudo code" for the onrowdatabound event handler in gridview:
ArrayList linksClicked = new ArrayList();
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hl = (HyperLink)e.Row.FindControl("links");
if (hl != null)
{
linksClicked.Add(h1.ToString());
}
}
You should use ItemTemplate with LinkButton. In this button you can keep index or id like CommandArgument, also you easily catch event onClick and add index to your array. Use this sample.
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="hyperLinkButton" Text="link" PostBackUrl="youruri.com" runat="server"
CommandArgument="<%# Eval("SomeFieldYouNeedArguementFrom") %>" OnClick="hyperLinkButton_Click" >
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
protected void hyperLinkButton_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
string yourValue = btn.CommandArgument;
// do what you need here
}

How to disable button in row databound event in gridview?

i want to disable button in row data bound . when its text or value is 'Waiting for Approval'. im getting this error . Object reference not set to an instance of an object.// button.Enabled = true;
protected void GridCategoryWise_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
Button button = (Button)e.Row.FindControl("btnReportedlink");
string Id =((DataRowView)e.Row.DataItem)["ReportLinks"].ToString();
if (Id == "Waiting for Approval")
{
button.Enabled = false;
}
else
{
button.Enabled = true;
}
}
my aspx
<asp:TemplateField HeaderText="Reportd Link" ItemStyle-HorizontalAlign="center" >
<ItemTemplate>
<button onclick="window.open('<%#Eval("ReportLinks")%>', '_blank');" title='<%#Eval("ReportLinks")%>' id="btnReportedlink" runat="server"> Link</button>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
Why are you using the HTML <button /> element ? use <asp:button /> web server control from asp.net for a better control over reading and disabling the server controls.
Use the OnClientClick property to specify additional client-side script that executes when a Button control's Click event is raised.
<ItemTemplate>
<asp:button onclientclick="javascript:window.open('<%#Eval("ReportLinks")%>', '_blank');"
text='<%#Eval("ReportLinks")%>' id="btnReportedlink" runat="server"/>
</ItemTemplate>
With the above setup, now you will be able to access the button in row data bound event with NO more object references error.
use DataBinder work fine
protected void GridCategoryWise_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
Button button = (Button)e.Row.FindControl("btnReportedlink");
string Id = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ReportLinks"));
if (Id == "Waiting for Approval")
{
button.Enabled = false;
}
else
{
button.Enabled = true;
}
}

Setting Button in listview Visibility to False if query result is "0"

I am building a web application in ASP.net and I have a little problem.
I have a LISTVIEW to display data from a data source, and in that listview I have included a BUTTON in every row to be visible if the result of the query in the Page_load is 0.
The Query works, but I don't know how to select the button in the query.
I have tried
ListView1.FindControl("hiddenButton").visible = false;
this is the buttons code
<asp:Button ID="hiddenButton" runat="server" CommandArgument ='<%# Eval("ProfileId") %>' Text="Add Friend" CssClass="btn btn-info pull-right" OnClick="addFriend_Click" Width="105px" allign="right"/>
But its not working.
You can do this in ItemDataBound event:-
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType==ListViewItemType.DataItem)
{
if (YourCondition)
{
Button hdn = (Button)e.Item.FindControl("hiddenButton");
hdn.Visible = false;
}
}
}
You need to associate this event handler in your mark-up(if not already done):-
<asp:ListView ID="ListView1" OnItemDataBound="ListView1_ItemDataBound">
</asp:ListView>
You can use ItemDataBound event To set Buttons visible to True/False
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Button hiddenButton=(Button) dataItem.FindControl("hiddenButton");
hiddenButton.Visible = false;
}
}

run c# code from 2 buttons in a gridview

I have a Gridview with two buttons and I want to be able to run code when the user clicks on them. I have tried using Row_command and setting a CommandName on the buttons , but i am going round in circles! Help!
I cant seem to get the username name from the first cell in order to search for the user in the rowcommand:
protected void gridview_search_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "unlock_account")
{
string user = gridview_search.SelectedRow.Cells[0].ToString();
//run code when user is obtained
}
}
should be able to set the event-handlers for those buttons to whichever function you are trying to run.
dgvbutton1.clickevent = dosomething();
dgvbutton2.clickevent = dosomething();
something along those lines should work
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AddButton" runat="server"
CommandName="AddToCart"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Text="Add to Cart" />
</ItemTemplate>
</asp:TemplateField>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddToCart")
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = GridView1.Rows[index];
// Add code here to add the item to the shopping cart.
}
}
My syntax was wrong for the command name part. I finally figured it out and then was able to get selected value from the gridview using DataKeys and was then able to
send this value to unlock user account.
protected void gridview_search_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName.CompareTo("unlock_account") == 0)
{
int user = (int)gridview_search.DataKeys[Convert.ToInt32(e.CommandArgument)].Value;
//unlock account method
userObj.unlockAccount(user);
}
}

Categories

Resources