jump to navigation

Things you never did with your code in school that you MUST do 15 August 2012

Posted by golda972 in Uncategorized.
add a comment

I’ve never had much patience for top ten lists. So this may or may not end up being ten items. For today, I have two items.

In any case – let’s face it: coding in school is not the same as coding for real. OK, Capstone/final project may approach real life. That really depends on the project.

So if you’re still running a one-man ship after graduation, here are some things that you will really, really hug yourself for doing when things go wrong:

  1. Version control. This does not need to cost money. Subversion is a wonderful product with plugins that you can use to integrate it with Windows and Visual Studio, if those are the tools you know from school. Start by installing Tortoise for SVN – then, right-click on the folders of your project and you’ll see the menu that lets you add them to version control. Then, install AnkhSVN for integration with Visual Studio.
  2. Error logging. Code that will be developed on one computer, tested on a second, and run on many others will not provide you with the feedback you need to debug it. Unless you tell it to give you that feedback. Some experience with school projects may have left you with an inclination to wrap things in a try block and pair that with an empty catch, so that when your code fails no one will know. Outgrow that. At the very least, write your errors to a text file (include the exception’s Message and StackTrace).

גישה לפקדים בתוך TemplateField 6 December 2011

Posted by golda972 in Uncategorized.
add a comment

שאלה מחברת הסדנא מלכה:

 יש לי גריד ובתוכו TemplateField ובתוכו כמה רכיבים ItemTemplate.

אחד הרכיבים הוא Asp:Label שאני שופכת אליו תוכן של שדה מהDB, שהוא שדה שמהווה שרשור של כמה שורות מטבלה. השורות מופרדות בפסיקים. אני רוצה בקוד להפוך כל פסיק לתגית <br /> כדי שיציג את התוכן בכמה שורות.

האם יש דרך לגשת דרך הקוד לLabel הזה?

 

תשובת הסדנא בדוט נט:

המקום בשביל גישה לשדות של שורה הוא בevent של RowDataBound, בו בeventArgs יש לך את השורה ואז את יכולה לחפש בתוכו את הפקד שלך:

 

    <!– .aspx –>

    <asp:GridView
ID=”gv”
runat=”server”
OnRowDataBound=”gv_RowDataBound”>

        <Columns>

            <asp:TemplateField>

                <ItemTemplate>

                    <asp:Label
ID=”l”
runat=”server”
/>

                </ItemTemplate>

            </asp:TemplateField>

        </Columns>

    </asp:GridView>

 

    //in CodeBehind (.aspx.cs)

    protected
void gv_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        Label l = e.Row.FindControl(“l”) as
Label;

 

        if (l != null)

            l.Text = “hi”;

    }

 

למה זה קורה: כי יש אחד בID הזה לכל שורה ושורה, מה שגורם למצב שאין דרך לגשת לפקד סתם עפ”י השם שלו.

אם יש לך גם שורות header וfooter הפקד לא יימצא ולכן הבדיקה לnull. אפשר באותה מידה לבדוק את e.Row.RowType.

 

מקווה שזה מספיק ברור.

sq33G